得到这样的名字用户信息,电子邮件ID,从等身份验证令牌在.NET后端的Azure移动服务 [英] Get user info like name, email Id etc from authentication token in .NET Backend Azure Mobile Service

查看:112
本文介绍了得到这样的名字用户信息,电子邮件ID,从等身份验证令牌在.NET后端的Azure移动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Azure移动服务,以验证添加到我的Windows商店应用。继<一个href=\"https://azure.microsoft.com/en-in/documentation/articles/mobile-services-dotnet-backend-windows-universal-dotnet-get-started-users/\"相对=nofollow>从移动服务文档此文章中,我能够获得用户ID以及 MobileServiceAuthenticationToken (无论是谷歌以及微软账户)

我的问题是我如何获得的姓名,电子邮件标识等使用 MobileServiceAuthenticationToken 以类似用户信息 .NET 后端移动服务。我通过各种文章解释了如何在的JavaScript 后端移动服务做,但找不到任何在 C#.NET + 后端移动服务。

任何指针AP preciated。

感谢


解决方案

有关使用获得认证令牌从净后端的Azure移动服务用户信息:


  1. 访问设置为不同供应商的身份验证您的移动服务<一个href=\"https://azure.microsoft.com/en-in/documentation/articles/mobile-services-dotnet-backend-windows-universal-dotnet-get-started-users/\"相对=nofollow>此链接 - 我这样做是为微软,谷歌和Facebook

  2. 添加一个新的控制器到移动服务,并添加以下code:


    公共类UserInfoController:ApiController
    {
        公共ApiServices服务{搞定;组; }

  [AuthorizeLevel(AuthorizationLevel.User)
    公共异步任务&LT; JObject&GT; GetUserInfo()
    {
        //获取当前登录的用户
        ServiceUser用户= this.User为ServiceUser;
        如果(用户== NULL)
        {
            抛出新的InvalidOperationException异常(这只能通过验证客户端被称为);
        }        //获取身份信息进行登录的用户当前
        VAR身份=等待user.GetIdentitiesAsync();
        VAR的结果=新JObject();        //检查用户使用Facebook的身份提供商已记录
        变种FB = identities.OfType&LT; FacebookCredentials&GT;()FirstOrDefault()。
        如果(FB!= NULL)
        {
            VAR的accessToken = fb.AccessToken;
            result.Add(脸谱,等待GetProviderInfo(https://graph.facebook.com/me?access_token=+的accessToken));
        }        //检查用户使用Microsoft身份提供商已记录
        变种毫秒= identities.OfType&所述; MicrosoftAccountCredentials方式&gt;()FirstOrDefault();
        如果(MS!= NULL)
        {
            VAR的accessToken = ms.AccessToken;
            result.Add(微软,等待GetProviderInfo(https://apis.live.net/v5.0/me/?method=GET&access_token=+的accessToken));
        }        //检查用户在使用谷歌作为身份提供商已记录
        VAR谷歌= identities.OfType&LT; GoogleCredentials&GT;()FirstOrDefault()。
        如果(谷歌!= NULL)
        {
            VAR的accessToken = google.AccessToken;
            result.Add(谷歌,等待GetProviderInfo(https://www.googleapis.com/oauth2/v1/userinfo?access_token=+的accessToken));
        }        返回结果;
    }    私人异步任务&LT; JToken&GT; GetProviderInfo(字符串URL)
    {
        变种C =新的HttpClient();
        VAR RESP =等待c.GetAsync(URL);
        resp.EnsureSuccessStatus code();
        返回JToken.Parse(等待resp.Content.ReadAsStringAsync());
    }
}

一旦完成发布移动服务。以上code获取当前登录的用户,然后根据哪个身份验证提供用户选择(微软,Facebook或谷歌)它使该身份提供者的用户配置文件API的调用,并获取用户信息的身份信息。

<醇开始=3>
  • 添加以下code客户端应用程序,使移动服务GetUserInfo方法的调用:


  • VAR用户=等待App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);
    VAR USERINFO =等待App.MobileService.InvokeApiAsync(用户信息,HttpMethod.Get,NULL);

    不过,如果你的应用需要比这更可以在登录时要求额外的范围,在蔚蓝的门户网站设置在移动服务配置选项卡上的MS_FacebookScope和MS_MicrosoftScope应用程序设置这样只会带来如姓名,性别等一些基本的用户信息

    您可以从中得到更多这方面的详细信息。<一个href=\"http://azure.microsoft.com/en-us/blog/custom-login-scopes-single-sign-on-new-asp-net-web-api-updates-to-the-azure-mobile-services-net-backend/\"相对=nofollow>优秀文章

    I am using Azure Mobile Service to add authentication to my Windows Store app. Following this article from Mobile Services documentation I am able to get the UserId as well as MobileServiceAuthenticationToken (both for Google as well as Microsoft Account)

    My question is how do I get user info like name, email Id etc. using MobileServiceAuthenticationToken in a .NET backend mobile service. I have gone through various articles explaining how this is done in Javascript Backend mobile service but couldn't find anything properly implemented in C# + .NET backend mobile service.

    Any pointers appreciated.

    Thanks

    解决方案

    For getting user info using authentication token from .Net Backend Azure Mobile Service:

    1. Setup your mobile service with authentication for different providers by visiting this link - I did this for Microsoft, Google and Facebook.
    2. Add a new controller to the mobile service and add the following code:

    public class UserInfoController : ApiController { public ApiServices Services { get; set; }

        [AuthorizeLevel(AuthorizationLevel.User)]
        public async Task<JObject> GetUserInfo()
        {
            //Get the current logged in user
            ServiceUser user = this.User as ServiceUser;
            if (user == null)
            {
                throw new InvalidOperationException("This can only be called by authenticated clients");
            }
    
            //Get Identity Information for the current logged in user
            var identities = await user.GetIdentitiesAsync();
            var result = new JObject();
    
            //Check if the user has logged in using Facebook as Identity provider
            var fb = identities.OfType<FacebookCredentials>().FirstOrDefault();
            if (fb != null)
            {
                var accessToken = fb.AccessToken;
                result.Add("facebook", await GetProviderInfo("https://graph.facebook.com/me?access_token=" + accessToken));
            }
    
            //Check if the user has logged in using Microsoft Identity provider
            var ms = identities.OfType<MicrosoftAccountCredentials>().FirstOrDefault();
            if (ms != null)
            {
                var accessToken = ms.AccessToken;
                result.Add("microsoft", await GetProviderInfo("https://apis.live.net/v5.0/me/?method=GET&access_token=" + accessToken));
            }
    
            //Check if the user has logged in using Google as Identity provider
            var google = identities.OfType<GoogleCredentials>().FirstOrDefault();
            if (google != null)
            {
                var accessToken = google.AccessToken;
                result.Add("google", await GetProviderInfo("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + accessToken));
            }
    
            return result;
        }
    
        private async Task<JToken> GetProviderInfo(string url)
        {
            var c = new HttpClient();
            var resp = await c.GetAsync(url);
            resp.EnsureSuccessStatusCode();
            return JToken.Parse(await resp.Content.ReadAsStringAsync());
        }
    }
    

    Once done publish the mobile service. The above code gets the identity information for the current logged in user and then depending upon which authentication provider user has chosen (Microsoft, Facebook or Google) it makes a call to that identity provider's user profile API and gets the user information.

    1. Add the following code in the client application to make a call to GetUserInfo method in mobile service:

    var user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);                  
    var userInfo = await App.MobileService.InvokeApiAsync("userInfo", HttpMethod.Get, null);
    

    However this will bring only some basic user information like name, gender etc. if your application requires more than that you can request additional scopes during the login, by setting the MS_FacebookScope and MS_MicrosoftScope app settings in mobile service Configure Tab in azure portal.

    You can get more detailed information on this from this excellent article

    这篇关于得到这样的名字用户信息,电子邮件ID,从等身份验证令牌在.NET后端的Azure移动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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