使用ASP.NET MVC 5导入Gmail联系人默认UseGoogleAuthentication()

时间:2017-04-03 10:32:54

标签: asp.net-mvc model-view-controller

您好我使用默认MVC Google身份验证使用Google登录我已登录但我也要导入Gmail联系人列表

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
    ClientId = "<<Client Id>>",
    ClientSecret = "<<Client Secret>>",
});

我尝试了很多方法,比如我尝试下面但是没有成功

var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
{
    ClientId = "<<Client Id>>",
    ClientSecret = "<<Client Secret>>",

    Provider = new GoogleOAuth2AuthenticationProvider()
    {
        OnAuthenticated = async context =>
        {
            //context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString()));
            //context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString()));
            //context.Identity.AddClaim(new Claim("contacts", context.User.GetValue("contacts").ToString()));

            var userDetail = context.User;
            context.Identity.AddClaim(new Claim(ClaimTypes.Name, context.Identity.FindFirstValue(ClaimTypes.Name)));
            context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Identity.FindFirstValue(ClaimTypes.Email)));

            var gender = userDetail.Value<string>("gender");
            context.Identity.AddClaim(new Claim(ClaimTypes.Gender, gender));

            var picture = userDetail.Value<string>("picture");
            context.Identity.AddClaim(new Claim("picture", picture));

            //return Task.FromResult(0);
        }
    }
};

//googleOAuth2AuthenticationOptions.Scope.Add("email");
googleOAuth2AuthenticationOptions.Scope.Add("https://www.googleapis.com/auth/plus.login");
googleOAuth2AuthenticationOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);

有没有办法在使用此app.UseGoogleAuthentication()函数的Owin中间件中使用Google Apis,如Google.Contacts,Google.GData.Client和Google.GData.Extensions?

0 个答案:

没有答案