我是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,但在我的案例中没有帮助。
答案 0 :(得分:3)
这类似于: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"});
这应该会修复谷歌提供商和所有其他人。
答案 1 :(得分:2)
试试这个可行。它适用于我的案例
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = "YourClintId",
ClientSecret = "YourSecretKey",
CallbackPath = new PathString("/Account/ExternalLoginCallback")
});
答案 2 :(得分:1)
我在设置Owin / Katana / Oath等方面遇到了问题。
简而言之......
GoogleOAuth2AuthenticationOptions.CallbackPath
保留为默认长期......
清除浏览器历史记录。我过去几天一直在努力学习OWIN / Katana等,并且已经在Google Developer Console和我的代码中进行了许多配置更改。我偶尔会得到“白屏”而无法让调试器在我的ExternalLoginCallback()
函数中命中代码。清除我的浏览器历史似乎解决了这个问题。
无需设置GoogleOAuth2AuthenticationOptions.CallbackPath
,将其保留为默认signin-google
。
我正在本地测试所以我已将我的Google凭据设置为(将端口号替换为您正在使用的端口号!)
授权Javascript起源:“https://localhost:44353”
授权重定向URI:“https://localhost:44353/signin-google”和“https://localhost:44353/Account/ExternalLoginCallback”
如果有人有兴趣那么过于冗长的代码
<强> Startup.Auth.cs 强>
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
var cookieAuthenticationProvider = new CookieAuthenticationProvider();
var cookieAuthenticationOptions = new CookieAuthenticationOptions();
cookieAuthenticationOptions.AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie;
cookieAuthenticationOptions.LoginPath = new PathString("/Account/Login");
cookieAuthenticationOptions.Provider = cookieAuthenticationProvider;
app.UseCookieAuthentication(cookieAuthenticationOptions);
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions();
googleOAuth2AuthenticationOptions.ClientId = "TODO : add client id";
googleOAuth2AuthenticationOptions.ClientSecret = "TODO : add secret";
app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
}
}
用户点击“使用Google登录”时执行的功能。 provider
将是“Google”
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public void ExternalLogin(string provider)
{
var properties = new Microsoft.Owin.Security.AuthenticationProperties();
properties.RedirectUri = Url.Action("ExternalLoginCallback", "Account");
HttpContext.GetOwinContext().Authentication.Challenge(properties, provider);
}
用户从Google返回时将执行的功能。
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback()
{
var loginInfo = await Microsoft.Owin.Security.AuthenticationManagerExtensions.GetExternalLoginInfoAsync(HttpContext.GetOwinContext().Authentication);
if (loginInfo == null)
{
throw new NotImplementedException();
}
var signInResult = await this.SignInManager.ExternalSignInAsync(loginInfo, false);
if (signInResult == Microsoft.AspNet.Identity.Owin.SignInStatus.Success)
{
return RedirectToAction("Index", "Home");
}
if (signInResult == Microsoft.AspNet.Identity.Owin.SignInStatus.RequiresVerification)
{
// ...
}
/// etc...
}
答案 3 :(得分:0)
尝试使用相同的代码,但将if (CallType_FromJSON == Constants.HttpMethod.Post)
{ Message = Client.PostAsync(CompleteURL, Content).Result; }
else if (CallType_FromJSON == Constants.HttpMethod.Get)
{ Message = Client.GetAsync(CompleteURL, HttpCompletionOption.ResponseContentRead).Result; }
string Description = string.Empty;
更改为var = httpresult.content.readasstringasyhnc();
并将其注册到应用中的授权重定向URI中。
答案 4 :(得分:-1)
您已添加以下内容,以便在RouteConfig.cs中映射路线,因为Google会将响应发送到您的域/ signin-google。
public static void RegisterRoutes(RouteCollection routes)
{
...
routes.MapRoute(name: "signin-google", url: "signin-google", defaults: new { controller = "Account", action = "ExternalLoginCallback" });
}