asp.net-mvc-4 – GoogleOauth2问题获取Internal Server 500错误

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-4 – GoogleOauth2问题获取Internal Server 500错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我决定尝试添加新的Google Oauth2中间件,它几乎破坏了一切。这是我的提供者配置从startup.auth.cs ..打开时,所有的提供程序,包括谷歌提供商获得一个500内部服务器在挑战。然而,内部服务器错误的细节是不可用的,我不知道如何打开任何调试或跟踪的Katana中间件。似乎像我们一样急于把谷歌的Oauth中间件送到门外。
  1. //// GOOGLE
  2. var googleOptions = new GoogleOAuth2AuthenticationOptions
  3. {
  4. ClientId = "228",ClientSecret = "k",CallbackPath = new PathString("/users/epsignin")
  5. SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,Provider = new GoogleOAuth2AuthenticationProvider
  6. {
  7. OnAuthenticated = context =>
  8. {
  9. foreach (var x in context.User)
  10. {
  11. string claimType = string.Format("urn:google:{0}",x.Key);
  12. string claimValue = x.Value.ToString();
  13. if (!context.Identity.HasClaim(claimType,claimValue))
  14. context.Identity.AddClaim(new Claim(claimType,claimValue,XmlSchemaString,"Google"));
  15. }
  16. return Task.FromResult(0);
  17. }
  18. }
  19. };
  20.  
  21. app.UseGoogleAuthentication(googleOptions);

ActionMethod代码

  1. [AllowAnonymous]
  2. public ActionResult ExternalProviderSignIn(string provider,string returnUrl)
  3. {
  4. var ctx = Request.GetOwinContext();
  5. ctx.Authentication.Challenge(
  6. new AuthenticationProperties
  7. {
  8. RedirectUri = Url.Action("EPSignIn",new { provider })
  9. },provider);
  10. return new HttpUnauthorizedResult();
  11. }

解决方法

这花了我几个小时才弄清楚,但问题是由@CrazyCoder提到的CallbackPath。我意识到CallbackPath在public void ConfigureAuth(IAppBuilder app)中必须与在ChallengeResult中设置时不同。如果它们相同,则在OWIN中抛出500个错误

我的代码是用于ConfigureAuth(IAppBuilder app)的

  1. var googleOptions = new Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationOptions
  2. {
  3. ClientId = "xxx",ClientSecret = "yyy",CallbackPath = new PathString("/callbacks/google"),//this is never called by MVC,but needs to be registered at your oAuth provider
  4.  
  5. Provider = new GoogleOAuth2AuthenticationProvider
  6. {
  7. OnAuthenticated = (context) =>
  8. {
  9. context.Identity.AddClaim(new Claim("picture",context.User.GetValue("picture").ToString()));
  10. context.Identity.AddClaim(new Claim("profile",context.User.GetValue("profile").ToString()));
  11. return Task.FromResult(0);
  12. }
  13. }
  14. };
  15.  
  16. googleOptions.Scope.Add("email");
  17.  
  18. app.UseGoogleAuthentication(googleOptions);

我的’回调’控制器代码是:

  1. // GET: /callbacks/googlereturn - callback Action
  2. [AllowAnonymous]
  3. public async Task<ActionResult> googlereturn()
  4. {
  5. return View();
  6. }
  7.  
  8. //POST: /Account/GooglePlus
  9. public ActionResult GooglePlus()
  10. {
  11. return new ChallengeResult("Google",Request.Url.GetLeftPart(UriPartial.Authority) + "/callbacks/googlereturn",null);
  12. //Needs to be a path to an Action that will handle the oAuth Provider callback
  13. }
  14.  
  15. private class ChallengeResult : HttpUnauthorizedResult
  16. {
  17. public ChallengeResult(string provider,string redirectUri)
  18. : this(provider,redirectUri,null)
  19. {
  20. }
  21.  
  22. public ChallengeResult(string provider,string redirectUri,string userId)
  23. {
  24. LoginProvider = provider;
  25. RedirectUri = redirectUri;
  26. UserId = userId;
  27. }
  28.  
  29. public string LoginProvider { get; set; }
  30. public string RedirectUri { get; set; }
  31. public string UserId { get; set; }
  32.  
  33. public override void ExecuteResult(ControllerContext context)
  34. {
  35. var properties = new AuthenticationProperties() { RedirectUri = RedirectUri };
  36. if (UserId != null)
  37. {
  38. properties.Dictionary[XsrfKey] = UserId;
  39. }
  40. context.HttpContext.GetOwinContext().Authentication.Challenge(properties,LoginProvider);
  41. }
  42. }

>回调/谷歌似乎由OWIN处理
回调/ googlereturn似乎由MVC处理

它现在都在工作,虽然很想知道发生在帽子下的事情

除非另有要求,否则我的建议是让OWIN使用默认的重定向路径,并确保不要自己使用它们。

猜你在找的asp.Net相关文章