asp.net-mvc-4 – GoogleOauth2问题获取Internal Server 500错误
发布时间:2020-05-25 04:09:23 所属栏目:asp.Net 来源:互联网
导读:我决定尝试添加新的Google Oauth2中间件,它几乎破坏了一切。这是我的提供者配置从startup.auth.cs ..打开时,所有的提供程序,包括谷歌提供商获得一个500内部服务器在挑战。然而,内部服务器错误的细节是不可用的,我不知道如何打开任何调试或跟踪的Katana中
|
我决定尝试添加新的Google Oauth2中间件,它几乎破坏了一切。这是我的提供者配置从startup.auth.cs ..打开时,所有的提供程序,包括谷歌提供商获得一个500内部服务器在挑战。然而,内部服务器错误的细节是不可用的,我不知道如何打开任何调试或跟踪的Katana中间件。似乎像我们一样急于把谷歌的Oauth中间件送到门外。 //// GOOGLE
var googleOptions = new GoogleOAuth2AuthenticationOptions
{
ClientId = "228",ClientSecret = "k",CallbackPath = new PathString("/users/epsignin")
SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,Provider = new GoogleOAuth2AuthenticationProvider
{
OnAuthenticated = context =>
{
foreach (var x in context.User)
{
string claimType = string.Format("urn:google:{0}",x.Key);
string claimValue = x.Value.ToString();
if (!context.Identity.HasClaim(claimType,claimValue))
context.Identity.AddClaim(new Claim(claimType,claimValue,XmlSchemaString,"Google"));
}
return Task.FromResult(0);
}
}
};
app.UseGoogleAuthentication(googleOptions);
ActionMethod代码: [AllowAnonymous]
public ActionResult ExternalProviderSignIn(string provider,string returnUrl)
{
var ctx = Request.GetOwinContext();
ctx.Authentication.Challenge(
new AuthenticationProperties
{
RedirectUri = Url.Action("EPSignIn",new { provider })
},provider);
return new HttpUnauthorizedResult();
}
解决方法这花了我几个小时才弄清楚,但问题是由@CrazyCoder提到的CallbackPath。我意识到CallbackPath在public void ConfigureAuth(IAppBuilder app)中必须与在ChallengeResult中设置时不同。如果它们相同,则在OWIN中抛出500个错误。我的代码是用于ConfigureAuth(IAppBuilder app)的 var googleOptions = new Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationOptions
{
ClientId = "xxx",ClientSecret = "yyy",CallbackPath = new PathString("/callbacks/google"),//this is never called by MVC,but needs to be registered at your oAuth provider
Provider = new GoogleOAuth2AuthenticationProvider
{
OnAuthenticated = (context) =>
{
context.Identity.AddClaim(new Claim("picture",context.User.GetValue("picture").ToString()));
context.Identity.AddClaim(new Claim("profile",context.User.GetValue("profile").ToString()));
return Task.FromResult(0);
}
}
};
googleOptions.Scope.Add("email");
app.UseGoogleAuthentication(googleOptions);
我的’回调’控制器代码是: // GET: /callbacks/googlereturn - callback Action
[AllowAnonymous]
public async Task<ActionResult> googlereturn()
{
return View();
}
//POST: /Account/GooglePlus
public ActionResult GooglePlus()
{
return new ChallengeResult("Google",Request.Url.GetLeftPart(UriPartial.Authority) + "/callbacks/googlereturn",null);
//Needs to be a path to an Action that will handle the oAuth Provider callback
}
private class ChallengeResult : HttpUnauthorizedResult
{
public ChallengeResult(string provider,string redirectUri)
: this(provider,redirectUri,null)
{
}
public ChallengeResult(string provider,string redirectUri,string userId)
{
LoginProvider = provider;
RedirectUri = redirectUri;
UserId = userId;
}
public string LoginProvider { get; set; }
public string RedirectUri { get; set; }
public string UserId { get; set; }
public override void ExecuteResult(ControllerContext context)
{
var properties = new AuthenticationProperties() { RedirectUri = RedirectUri };
if (UserId != null)
{
properties.Dictionary[XsrfKey] = UserId;
}
context.HttpContext.GetOwinContext().Authentication.Challenge(properties,LoginProvider);
}
}
>回调/谷歌似乎由OWIN处理 它现在都在工作,虽然很想知道发生在帽子下的事情 除非另有要求,否则我的建议是让OWIN使用默认的重定向路径,并确保不要自己使用它们。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 如何在Html.RenderAction(MVC3)中发送模型对
- asp.net – 自动化Web应用前端性能测试的替代方案
- asp.net-mvc – 使用asp.net mvc操作过滤器的奇怪行为Attri
- asp.net-mvc – 重构经典ASP的最佳做法?
- asp.net – 如何检测用户操作系统
- asp.net – 我可以使用一种模式来编辑MVC3应用程序中的下拉
- asp.net-mvc-3 – 大于或等于今天日期验证注释在MVC3
- 在asp.net页面中的CSS粘贴页脚
- asp.net-mvc – 无法通过kendo上传从一个视图到另一个视图的
- asp.net-mvc-5 – 如何让Glimpse与EF6合作?
推荐文章
站长推荐
- asp.net-mvc-3 – 在F#中的ViewBag动态对象上设置
- asp.net-mvc-3 – MVC 3 $.ajax – 响应似乎是从
- .net – DataAnnotations与IDataErrorInfo
- asp.net-mvc – 检查上传的文件是否是C#ASP.NET
- 是否有asp.net的控制台日志?
- ASP.NET – UpdatePanel和JavaScript
- asp.net – 如何在MVC4 Web API中自定义JSON序列
- webforms – ASP.Net Core 1.0是否支持WebForm项
- asp.net – 查找应用程序根URL而不使用
- 学习:正则表达式的基本语法
热点阅读
