首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >获取内部服务器500错误时出现GoogleOauth2问题

获取内部服务器500错误时出现GoogleOauth2问题
EN

Stack Overflow用户
提问于 2014-02-23 12:14:15
回答 5查看 9.1K关注 0票数 19

我决定试一试新的谷歌Oauth2中间件,它几乎打破了一切。这是我从startup.auth.cs获得的提供程序配置。打开后,包括google提供商在内的所有提供商都会在Challenge上获得一个500内部服务器。但是,内部服务器错误的详细信息不可用,我也不知道如何打开Katana中间件的调试或跟踪。在我看来,他们似乎急于将google Oauth中间件推出。

代码语言:javascript
复制
  //// 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代码:

代码语言:javascript
复制
 [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();
    }
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-05-12 01:40:32

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

我为ConfigureAuth(IAppBuilder app)编写的代码是

代码语言:javascript
复制
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);

我的“回调”控制器代码是:

代码语言:javascript
复制
// 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);
    }
}

  • callbacks/google似乎由OWIN
  • callbacks/googlereturn处理似乎由MVC

处理

现在一切都正常了,尽管我很想知道到底发生了什么

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

票数 28
EN

Stack Overflow用户

发布于 2015-08-05 15:46:05

不需要在UseGoogleAuthentication中指定CallbackPath

代码语言:javascript
复制
CallbackPath = new PathString("/Account/ExternalLoginCallback")

只需保留授权重定向URIs的谷歌设置为:

http(s)://yoururl:orPort/signin-google

Owin在内部处理signin-google并重定向到ChallengeResult类的代码中提到的redirectUri。即Account/ExternalLoginCallback。

票数 7
EN

Stack Overflow用户

发布于 2016-01-12 02:04:03

从教程中得到了它的工作香草,只需一个简单的更改-只需将其发布到此方法的任何块。我认为在这种情况下与oauth2相关的问题在最新的模板/apis中得到了很大程度的充实-我的意思是,如果你是从头开始的,你可能会走运-继续阅读:

我刚刚完成了这个教程https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-deploy-aspnet-mvc-app-membership-oauth-sql-database/

并且还引用了http://blogs.msdn.com/b/webdev/archive/2014/07/02/changes-to-google-oauth-2-0-and-updates-in-google-middleware-for-3-0-0-rc-release.aspx

只有一个变化:它起作用了,但只有在最新版本的google+开发者站点中启用了google apis之后才能工作。

(只需转到google api库管理器,登录并在api目录中搜索google+ api)。

注意:对于我来说,默认情况下Google+接口是禁用的。

我没有做任何其他独特的事情。

干杯

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21964139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档