相关疑难解决方法(0)

ASP.Net MVC 5带范围的Google身份验证

我正在尝试让ASP.Net MVC 5 Google OAuth2身份验证正常运行.

当我在没有任何范围的情况下设置传入GoogleOauth2AuthenticationOptions时,我就能够成功登录.

var googlePlusOptions = new GoogleOAuth2AuthenticationOptions
{
    ClientId = googleClientId,
    ClientSecret = googleClientSecret,
    SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
    Provider = new GoogleOAuth2AuthenticationProvider()
    {
        OnAuthenticated = async ctx =>
        {
            ctx.Identity.AddClaim(new Claim("urn:tokens:googleplus:accesstoken", ctx.AccessToken));
        }
    },
};

app.UseGoogleAuthentication(googlePlusOptions);
Run Code Online (Sandbox Code Playgroud)

然后,此调用将返回一个设置了所有属性的ExternalLoginInfo对象

ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
Run Code Online (Sandbox Code Playgroud)

当我添加任何范围时,我没有得到任何返回的登录信息.它只是空的.

var googlePlusOptions = new GoogleOAuth2AuthenticationOptions
{
    ClientId = googleClientId,
    ClientSecret = googleClientSecret,
    SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
    Provider = new GoogleOAuth2AuthenticationProvider()
    {
        OnAuthenticated = async ctx =>
        {
            ctx.Identity.AddClaim(new Claim("urn:tokens:googleplus:accesstoken", ctx.AccessToken));
        }
    },
}; …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc oauth-2.0 google-oauth asp.net-mvc-5 asp.net-identity

8
推荐指数
2
解决办法
1万
查看次数

ASP.NET身份OWIN中间件Google OAuth2 AuthenticationManager SignIn无法正常工作

我创建了一个简单的ASP.NET MVC4网站来测试新的OWIN身份验证中间件,我决定从谷歌OAuth2开始,我已经在配置方面做了很多努力,但我已经设法让谷歌授权用户,我现在面临的问题是OWIN没有对用户进行身份验证.

我想我在网络配置中有适当的设置

<system.web>
     <authentication mode="None" />
</system.web>
<system.webServer>
     <modules>
        <remove name="FormsAuthenticationModule" />
     </modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

然后我在Startup类中有一个非常简单的配置

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
    }

    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
        // Enable the External Sign In Cookie.
        app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ExternalCookie);
        // Enable Google authentication.
        app.UseGoogleAuthentication(GetGoogleOptions());
    }

    private static GoogleOAuth2AuthenticationOptions GetGoogleOptions()
    {
        var reader = new KeyReader();
        var keys = reader.GetKey("google");
        var options = new GoogleOAuth2AuthenticationOptions()
        {
            ClientId = keys.Public,
            ClientSecret = keys.Private
        };
        return options;
    }
}
Run Code Online (Sandbox Code Playgroud)

在 …

c# asp.net asp.net-mvc asp.net-mvc-4

3
推荐指数
2
解决办法
3950
查看次数