gpt4 book ai didi

asp.net-mvc - 使用 GoogleOAuth2AuthenticationOptions 出现 redirect_uri_mismatch 错误

转载 作者:行者123 更新时间:2023-12-04 13:33:20 25 4
gpt4 key购买 nike

我正在尝试在 MVC 5 Web 应用程序中实现 Google 身份验证。身份验证工作正常,但我会检索个人资料和图片信息。

为此,我添加了一个 GoogleOAuth2AuthenticationOptions 对象来指定其他声明:

var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
{
ClientId = "xxxxxxxxxxxxxxxx",
ClientSecret = "xxxxxxxxxxxxxxxxx",
CallbackPath = new PathString("/Account/LoginCallback"),
Provider = new GoogleOAuth2AuthenticationProvider()
{
OnAuthenticated = async context =>
{
context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString()));
context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString()));
}
}
};

app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);

但它会导致生成错误的 URL:

http://admin.localhost.com/Account/LoginCallback&state=Gs-otJmI79bgWA3_qJzDcGziWnkRCOf7JRoCUDCIz0jv4IIvDdoZlZzVSq2kZxfaPFDmv9hbZGp5q1Aq8mpLPguKnCF31twHj8NQCMv_NrgZzvKwaelmZr_HwY_bdj8h1ICFrkGTKLJ1saEYDbFJ2CJxvDkyBL2iygQmTXQTs-aUiL4yWe5_7dZQOjP_mDUSW-GXns3wr7Okwkoj8VEITJTUz9nAbrBd_N_7puTMlHU&client_id=xxxxxxxxxxxxxxxx

没有“?”在参数之前,这会导致 redirect_uri_mismatch。

但是,当我简单地使用时:
app.UseGoogleAuthentication(
clientId : "xxxxxxxxxxxxxxxxxxx",
clientSecret : "xxxxxxxxxxxxxxxxx");

它正在工作。

任何的想法 ?

最佳答案

只用这么多。

var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
{
ClientId = "MYCLIENTID",
ClientSecret = "MYSECRET",
};
app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);

这个方法好像会自动使用地址中的signin-google请求。为了解决这个问题,在google控制台修改google回调位置指向这个地址。

添加 路由配置文件
 routes.MapRoute( name: "signin-google", url: "signin-google", defaults: new { controller = "Account", action = "LoginCallback" } ); 

关于asp.net-mvc - 使用 GoogleOAuth2AuthenticationOptions 出现 redirect_uri_mismatch 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25982095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com