我是 MVC 5 身份验证的新手。目前我试过 使用 Owin 的 Google 授权
startup.Auth.cs 中的代码
var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
{
ClientId = "Client-id",
ClientSecret = "secret-key",
CallbackPath = new PathString("/Account/ExternalLoginCallback"),
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()));
}
}
};
googleOAuth2AuthenticationOptions.Scope.Add("email");
app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
但它不会点击 ExternalLoginCallback Action 进行调试。
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
它停在/Account/ExternalLoginCallback?ReturnUrl=%2F 与空白的白色屏幕。
我不会发现这有什么问题。并找到类似的问题 Google Authentication using OWIN Oauth in MVC5 not hitting ExternalLoginCallback function ,但对我的情况没有帮助。
最佳答案
这类似于:Google Authentication using OWIN Oauth in MVC5 not hitting ExternalLoginCallback function
基本上,在开发人员仪表板中设置您的 google 应用程序以指向您的 */ExternalLoginCallback 方法。
保留 GoogleProvider 的默认回调路径。
var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
{
ClientId = "MYCLIENTID",
ClientSecret = "MYSECRET"
};
在RouteConfig中添加处理signin-google的路由:
routes.MapRoute(
name: "signin-google",
url: "signin-google",
defaults: new { controller = "[YOURCONTROLLLER]", action = "ExternalLoginCallback"});
这也应该修复谷歌提供商和所有其他人。
关于oauth - 为什么 MVC 5 Owin Oauth 没有点击/Account/ExternalLoginCallback 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24691992/