将C#Owin响应类型从代码更改为令牌

时间:2016-12-27 14:50:10

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

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

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

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

这会从Google返回错误。如果我将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,


        };

Google中间件设置:

 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项目的基本设置。

1 个答案:

答案 0 :(得分:0)

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

 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。

仍然存在问题。