Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在身份验证期间使用MVC5 GoogleAuthenticationOptions获取额外的用户配置文件信息_Asp.net Mvc_Authentication_Asp.net Mvc 5 - Fatal编程技术网

Asp.net mvc 在身份验证期间使用MVC5 GoogleAuthenticationOptions获取额外的用户配置文件信息

Asp.net mvc 在身份验证期间使用MVC5 GoogleAuthenticationOptions获取额外的用户配置文件信息,asp.net-mvc,authentication,asp.net-mvc-5,Asp.net Mvc,Authentication,Asp.net Mvc 5,当用户使用谷歌账号登录我的MVC5网站时,我试图获取用户的出生日期和头像/图标/照片/图片 我读过: 但这没有多大意义。。。我应该知道这件事吗 在我的Startup.Auth.cs文件中,我有以下代码段: var gProvider = new GoogleAuthenticationProvider { OnAuthenticated = context => { var claims = context.Identity.Claims.ToList(); return Task.Fro

当用户使用谷歌账号登录我的MVC5网站时,我试图获取用户的出生日期和头像/图标/照片/图片

我读过:

但这没有多大意义。。。我应该知道这件事吗

在我的Startup.Auth.cs文件中,我有以下代码段:

var gProvider = new GoogleAuthenticationProvider { OnAuthenticated = context => { var claims = context.Identity.Claims.ToList(); return Task.FromResult(0); } };
var gOptions = new GoogleAuthenticationOptions { Provider = gProvider };
app.UseGoogleAuthentication(gOptions);
claims变量包含5个项目:每个项目以


有谁能帮我指出我做错了什么或者我缺少了什么来获取我正在寻找的额外的配置文件数据吗?

简而言之,如果您必须使用OAuth 2.0与Google集成,而不是默认启用的Open ID

public class CustomGoogleProvider : GoogleOAuth2AuthenticationProvider
{
    public override Task Authenticated(GoogleOAuth2AuthenticatedContext context)
    {
        context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString()));
        context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString()));

        return base.Authenticated(context);
    }
}
并将其用作

    var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
    {
        ClientId = "{{Your Client Id}}",
        ClientSecret = "{{Your Client Secret}}",
        CallbackPath = new PathString("/Account/ExternalGoogleLoginCallback"),
        Provider = new CustomGoogleProvider(),
    };

    googleOAuth2AuthenticationOptions.Scope.Add("email");

    app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);

我详细描述了这个过程

简而言之,您必须使用OAuth 2.0与Google的集成,而不是默认启用的开放ID

public class CustomGoogleProvider : GoogleOAuth2AuthenticationProvider
{
    public override Task Authenticated(GoogleOAuth2AuthenticatedContext context)
    {
        context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString()));
        context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString()));

        return base.Authenticated(context);
    }
}
并将其用作

    var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
    {
        ClientId = "{{Your Client Id}}",
        ClientSecret = "{{Your Client Secret}}",
        CallbackPath = new PathString("/Account/ExternalGoogleLoginCallback"),
        Provider = new CustomGoogleProvider(),
    };

    googleOAuth2AuthenticationOptions.Scope.Add("email");

    app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
我详细描述了这个过程