【问题标题】:Change C# Owin response type from code to token将 C# Owin 响应类型从代码更改为令牌
【发布时间】:2016-12-27 14:50:10
【问题描述】:

我正在尝试使用 OWIN 外部登录到 Google/Facebook。

面临的问题是 owin 挑战不断将响应类型从令牌更改为代码。

挑战会生成以下 URL: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=client_dim&redirect_uri=mywebsite.com&scope=scope&state=state

这会从谷歌返回一个错误。如果我将 response_type 更改为 token (response_type=token) 就可以了。

这是 OAuth 选项

 OAuthOptions = new OAuthAuthorizationServerOptions
        {

            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider(PublicClientId),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),

            // In production mode set AllowInsecureHttp = false
            AllowInsecureHttp = true,


        };

谷歌中间件设置:

 app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
        {
            ClientId = "clientid",
            ClientSecret = "client secret",  
        }); 

这是挑战:

   var properties = new AuthenticationProperties() {   AllowRefresh = true, RedirectUri="mywebsite.co.za"  };


        Request.GetOwinContext().Authentication.Challenge(properties,LoginProvider);

        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
        response.RequestMessage = Request;
        return Task.FromResult(response);

OWIN 是通用 MVC API 项目的基本设置。

【问题讨论】:

    标签: c# oauth-2.0 owin google-oauth


    【解决方案1】:

    将response_type重写为token的解决方案如下:

     GoogleOAuth2AuthenticationOptions googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
            {
                ClientId = "clientid",
                ClientSecret = "secret",
    
                Provider = new GoogleOAuth2AuthenticationProvider
                {
                    OnApplyRedirect = context =>
                    {
                        string redirect = context.RedirectUri.Replace("response_type=code", "response_type=token");
                        context.Response.Redirect(redirect);
                    },
    
                },
            };
    
            app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
    

    问题仍然存在,如果 google OAuth 2.0 需要 response_type=token 为什么 Owin.google 提供程序使用 response_type=code。

    【讨论】:

      猜你喜欢
      • 2020-06-20
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多