与外部认证提供商从MVC5框架的OAuth获取ExtraData / OWin身份提供商 [英] Get ExtraData from MVC5 framework OAuth/OWin identity provider with external auth provider

查看:162
本文介绍了与外部认证提供商从MVC5框架的OAuth获取ExtraData / OWin身份提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用VS 2013 preVIEW新MVC5框架。

I'm trying to use the new MVC5 framework in VS 2013 preview.

会员资格认证框架已经改进,以 OWin

The membership authentication framework has been overhauled and replaced with OWin.

在我特别接通外部认证提供商谷歌AUTH。

In particular, I turned on external authentication provider Google auth.

这是很简单的事情。

只需反注释这一行: app.UseGoogleAuthentication(); 在新的默认MVC项目的App_Start目录中的文件Startup.Auth.cs

Simply uncomment this line: app.UseGoogleAuthentication(); in the Startup.Auth.cs file in the App_Start directory of the new default MVC project.

所以,我想访问额外数据来自身份验证提供者,例如URL到用户的头像在我的应用程序中显示。

So, I want access the "Extra Data" that comes from the Authentication provider, such as a url to the user's avatar to display in my application.

在对asp.net成员资格提供旧的OAuth的实施,有一位用在这里发现了这个ExtraData词典捕捉方式:<一href=\"http://msdn.microsoft.com/en-us/library/microsoft.aspnet.membership.openauth.providerdetails.extradata%28v=vs.111%29.aspx\">ProviderDetail.ExtraData物业。

Under the older OAuth implementation against asp.net Membership provider, there was a way to capture this using this ExtraData dictionary found here: ProviderDetail.ExtraData Property.

我无法找到有关的OAuth和OWin一起工作,以及如何如何访问这些额外的数据多文档。

I can't find much documentation about how OAuth and OWin work together and how to access this extra data.

任何人都可以告诉我吗?

Can anyone enlighten me?

推荐答案

最近我不得不访问谷歌个人资料的图片,以及这里是我如何解决这个问题?

Recently I had to get access to Google profile's picture as well and here is how I solve it...

如果您刚刚启用code app.UseGoogleAuthentication(); 在Startup.Auth.cs文件是不够的,因为在这种情况下,谷歌不返回有关配置文件的所有图片中的任何信息(或我不知道如何得到它)。

If you just enable the code app.UseGoogleAuthentication(); in the Startup.Auth.cs file it's not enough because in this case Google doesn't return any information about profile's picture at all (or I didn't figure out how to get it).

您真正需要使用的OAuth2整合,而不是通过默认启用打开ID是什么。这里是我做到了......

What you really need is using OAuth2 integration instead of Open ID that enabled by default. And here is how I did it...

你必须注册您的谷歌方面的应用,并得到客户端ID和客户端秘密的第一位。只要完成这件事,你可以走得更远(你以后会需要它)。详细情况怎么这里做到这一点

First of all you have to register your app on Google side and get "Client ID" and "Client secret". As soon as this done you can go further (you will need it later). Detailed information how to do this here.

替换 app.UseGoogleAuthentication();

    var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
    {
        ClientId = "<<CLIENT ID FROM GOOGLE>>",
        ClientSecret = "<<CLIENT SECRET FROM GOOGLE>>",
        CallbackPath = new PathString("/Account/ExternalGoogleLoginCallback"),
        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()));
            }
        }
    };

    googleOAuth2AuthenticationOptions.Scope.Add("email");

    app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);

这之后,你可以用code以访问个人资料的图片URL相同的方法进行任何其他属性。

After that you can use the code to get access to the profile's picture URL same way as for any other properties

var externalIdentity = HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
var pictureClaim = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type.Equals("picture"));
var pictureUrl = pictureClaim.Value;

这篇关于与外部认证提供商从MVC5框架的OAuth获取ExtraData / OWin身份提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆