Asp.net mvc 4 GoogleOauth2问题获取内部服务器500错误
我决定尝试一下新的GoogleOAuth2中间件,它几乎破坏了一切。这是我从startup.auth.cs获得的提供程序配置。。启用时,所有提供商(包括谷歌提供商)都会在挑战中获得500台内部服务器。但是,内部服务器错误的详细信息不可用,我不知道如何打开Katana中间件的任何调试或跟踪。在我看来,他们似乎急于推出GoogleOAuth中间件Asp.net mvc 4 GoogleOauth2问题获取内部服务器500错误,asp.net-mvc-4,asp.net-mvc-5,katana,Asp.net Mvc 4,Asp.net Mvc 5,Katana,我决定尝试一下新的GoogleOAuth2中间件,它几乎破坏了一切。这是我从startup.auth.cs获得的提供程序配置。。启用时,所有提供商(包括谷歌提供商)都会在挑战中获得500台内部服务器。但是,内部服务器错误的详细信息不可用,我不知道如何打开Katana中间件的任何调试或跟踪。在我看来,他们似乎急于推出GoogleOAuth中间件 //// GOOGLE var googleOptions = new GoogleOAuth2AuthenticationOptio
//// 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);
操作方法代码:
[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提到的
回调路径。我意识到public void ConfigureAuth(IAppBuilder app)
中的CallbackPath
必须与在ChallengeResult
中设置时不同。如果它们相同,OWIN中会抛出500错误
我的代码用于ConfigureAuth(IAppBuilder应用程序)
is
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);
}
}
//GET:/callbacks/googlereturn-回调操作
[异名]
公共异步任务googlereturn()
{
返回视图();
}
//POST:/Account/GooglePlus
公众行动结果Google Plus()
{
returnnewchallengeresult(“Google”,Request.Url.GetLeftPart(UriPartial.Authority)+“/callbacks/googlereturn”,null);
//需要是处理oAuth提供程序回调的操作的路径
}
私有类质询结果:HttpUnauthorizedResult
{
公共ChallengeResult(字符串提供程序、字符串重定向URI)
:此(提供程序、重定向URI、null)
{
}
public ChallengeResult(字符串提供程序、字符串重定向URI、字符串用户ID)
{
LoginProvider=提供者;
重定向URI=重定向URI;
UserId=UserId;
}
公共字符串登录提供程序{get;set;}
公共字符串重定向URI{get;set;}
公共字符串用户标识{get;set;}
公共覆盖无效ExecuteSult(ControllerContext上下文)
{
var properties=newauthenticationproperties(){RedirectUri=RedirectUri};
if(UserId!=null)
{
properties.Dictionary[XsrfKey]=UserId;
}
context.HttpContext.GetOwinContext().Authentication.Challenge(属性,LoginProvider);
}
}
- 回调/谷歌似乎由OWIN处理
- 回调/谷歌返回似乎由MVC处理
现在一切都在运行,不过我很想知道“引擎盖下”到底发生了什么
除非您有其他要求,否则我的建议是让OWIN使用默认重定向路径,并确保您自己不使用这些路径。为了简单起见,我使用默认的ASP.NET MVC 5模板和身份验证,但希望可以针对不同的使用情况对其进行修改
StartupAuth.cs
不要自定义重定向路径。不管怎么说,它被/signin google替换了,而我试图绕开它导致了“沉默”(不在调试器中)的内部服务器500错误
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = "whatevs.apps.googleusercontent.com",
ClientSecret = "whatevs_secrut",
Provider = new GoogleOAuth2AuthenticationProvider()
});
确保在api&auth
凭证重定向URI
部分中添加到
RouteConfig.cs
将路由添加到路由的永久重定向控制器操作。永久重定向是这里唯一足够的东西。仅仅直接指向回调URL是不够的
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Google API Sign-in",
url: "signin-google",
defaults: new { controller = "Account", action = "ExternalLoginCallbackRedirect" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
AccountController.cs
永久重定向到内置回调方法,您应该不会有问题
[AllowAnonymous]
public ActionResult ExternalLoginCallbackRedirect(string returnUrl)
{
return RedirectPermanent("/Account/ExternalLoginCallback");
}
GitHub上发布了一个模板项目供参考:到目前为止给出的答案让我走上了一条真正黑暗的道路,我希望我没有走过这条路。。。解决方案很简单,请确保以下三点匹配:
1) 在Google宣誓凭证()中:
- 重定向URI已被删除
(将端口固定到您正在使用的任何端口)
2) 在您的帐户控制器中
:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
return new ChallengeResult(provider,
Url.Action("ExternalLoginCallback", "Account",
new { ReturnUrl = returnUrl }));
}
请注意,操作是“ExternalLoginCallback”
3) 在您的应用程序\u Start\Startup.Auth.cs
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = "yourclientid.apps.googleusercontent.com",
ClientSecret = "yoursecret",
Provider = new GoogleOAuth2AuthenticationProvider(),
CallbackPath = new PathString("/Account/ExternalLoginCallback")
});
请注意,CallbackPath
与其他2个路径具有相同的PathString
最后,如果您仍然无法获取,请在应用程序Web.config
<authentication mode="None" />
获取有关此问题的更多详细信息。无需在UseGoogleAuthentication
中指定CallbackPath
:
CallbackPath = new PathString("/Account/ExternalLoginCallback")
只需将授权重定向uri
的谷歌设置保持为:
http(s)://你的URL:orPort/登录谷歌
Owin在内部处理登录google并重定向到您的ChallengeResult类代码中提到的重定向URI。它是Account/ExternalLoginCallback。通过一个简单的更改,从教程中获得了它的正常工作-只需将此发布给任何使用此方法的NUBE。我认为与oauth2相关的问题在最新的模板/API中得到了很大程度的充实-我的意思是,如果您是从零开始的,您可能很幸运-请继续阅读:
我刚刚做了这个教程
并引用了这一点
一个变化:
但只有在最新版本的google开发者网站中启用google+API后,它才起作用
(只需转到google api库管理器,登录并在api目录中搜索google+api即可)。
注意:对我来说,Google+api在默认情况下是禁用的
我没有做其他独特的事
干杯我不确定具体的问题是什么——你能试着为谷歌软件包设置符号,看看哪里出了问题吗。下面是一些为katana设置符号的说明-这似乎是回调路径的问题。为该提供程序设置回调路径时,是否可能为所有提供程序全局设置该属性。没有深入到src,只是一个想法…当你说所有提供商的全球支持=>DoYo时