我决定试一试新的谷歌Oauth2中间件,它几乎打破了一切。这是我从startup.auth.cs获得的提供程序配置。打开后,包括google提供商在内的所有提供商都会在Challenge上获得一个500内部服务器。但是,内部服务器错误的详细信息不可用,我也不知道如何打开Katana中间件的调试或跟踪。在我看来,他们似乎急于将google 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();
}
发布于 2014-05-12 01:40:32
我花了几个小时才弄明白,但问题是@CrazyCoder提到的CallbackPath
。我意识到public void ConfigureAuth(IAppBuilder app)
中的CallbackPath
必须与在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使用默认的重定向路径,并确保你自己不会使用它们。
发布于 2015-08-05 15:46:05
不需要在UseGoogleAuthentication
中指定CallbackPath
:
CallbackPath = new PathString("/Account/ExternalLoginCallback")
只需保留授权重定向URIs
的谷歌设置为:
http(s)://yoururl:orPort/signin-google
Owin在内部处理signin-google并重定向到ChallengeResult类的代码中提到的redirectUri。即Account/ExternalLoginCallback。
发布于 2016-01-12 02:04:03
从教程中得到了它的工作香草,只需一个简单的更改-只需将其发布到此方法的任何块。我认为在这种情况下与oauth2相关的问题在最新的模板/apis中得到了很大程度的充实-我的意思是,如果你是从头开始的,你可能会走运-继续阅读:
只有一个变化:它起作用了,但只有在最新版本的google+开发者站点中启用了google apis之后才能工作。
(只需转到google api库管理器,登录并在api目录中搜索google+ api)。
注意:对于我来说,默认情况下Google+接口是禁用的。
我没有做任何其他独特的事情。
干杯
https://stackoverflow.com/questions/21964139
复制相似问题