Using ASP.NET Live Connect API to Retrieve User Email Address

So, I'm pretty new to ASP.NET MVC and the Windows Live Connect API. Basically, I am trying to integrate Live input into my website. When users log in, Live asks for permission to provide certain information to my application, sends the user to the uri redirection specified in my application settings, added using the query string. Here, if the user first subscribes to the site, I want their basic information to be stored on my server (name, surname, email address). I was able to get their first and last name, but it’s hard for me to find out how to get their primary email address. I will explain what I have done so far.

I could not find a better way to integrate Live connect into an MVC application, so I did my best. I pointed out the controller action in my uri redirect, which takes the "code" of the request to create an HTTP message.

HttpRequest req = System.Web.HttpContext.Current.Request; string myAuthCode = req.QueryString["code"]; string myAppId = ConfigurationManager.AppSettings.Get("wll_appid"); string mySecret = ConfigurationManager.AppSettings.Get("wll_secret"); string postData = "client_id=" + myAppId + "&redirect_uri=http%3A%2F%2Fmscontestplatformtest.com%2FContestPlatform%2FUser%2FSignIn&client_secret=" + mySecret + "&code=" + myAuthCode + "&grant_type=authorization_code"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); WebRequest request = WebRequest.Create("https://oauth.live.com/token"); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; request.Method = "POST"; 

Get the string response in JSON format and extract access_token. Then I use this access token to create an HTTP GET call as follows.

 request = WebRequest.Create("https://apis.live.net/v5.0/me?access_token=" + r.access_token); response = request.GetResponse(); reader = new StreamReader(response.GetResponseStream()); string userInfo = reader.ReadToEnd(); 

The GET call above provides me this JSON string:

 { "id": "02b4b930697bbea1", "name": "Daniel Hines", "first_name": "Daniel", "last_name": "Hines", "link": "http://profile.live.com/cid-02b4b930697bbea1/", "gender": "male", "locale": "en_US", "updated_time": "2011-10-14T21:40:38+0000" } 

This is all publicly available information, and it's great, I have almost everything I need, except for the main email address. What GET call do I need to receive email?

Also, am I doing this right?

+6
source share
1 answer

Ok, I get it!

I just needed to add the "wl.emails" area to my link. Then my GET call will return its email addresses.

+5
source

Source: https://habr.com/ru/post/911069/


All Articles