Reputation: 21
I'm trying to implement OAuth authentication with Microsoft ID for my ASP.NET MVC4 web site. So far, the log in with Microsoft account works. But I need to get some extra user data (specifically, users account or preferred email address).
I'm using live.domains as my email service. So I created emails for my users with my domain. Now I want to enable those users to log in to the site using their email accounts (with OAuth).
Note: my web server doesn't support asp.net 4.5, so I'm stuck on 4.0, and can't use the LiveSDK for ASP.NET (as far as I know)...
The idea is something like is:
I created a custom Microsoft OAuth2Client, using the default as reference (default clients can be found on github), and specified the required scopes (wl.basic, wl.signin, wl.emails). The rest of the code is pretty much the same.
public MyMicrosoftClient(string appId, string appSecret)
: this(appId, appSecret, new string[] {"wl.basic", "wl.signin", "wl.emails"})
{
}
The GetUserData function is used to get the extra user data (like name, gender), and that works. Code from default client:
protected override IDictionary<string, string> GetUserData(string accessToken)
{
MicrosoftClientUserData graph;
var request =
WebRequest.Create("https://apis.live.net/v5.0/me?access_token=" + CustomHelpers.EscapeUriDataStringRfc3986(accessToken));
using (var response = request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
graph = JsonHelper.Deserialize<MicrosoftClientUserData>(responseStream);
}
}
var userData = new Dictionary<string, string>();
userData.Add("id", graph.Id == "" ? null : graph.Id);
userData.Add("username", graph.Name == "" ? null : graph.Name);
userData.Add("name", graph.Name == "" ? null : graph.Name);
userData.Add("link", graph.Link == null ? null : graph.Link.AbsoluteUri);
userData.Add("gender", graph.Gender == "" ? null : graph.Gender);
userData.Add("firstname", graph.FirstName == "" ? null : graph.FirstName);
userData.Add("lastname", graph.LastName == "" ? null : graph.LastName);
return userData;
}
My understanding is that this gets all the requested user data (based on specified scopes), so it should also contain the emails in the response. Unfortunately, even if it does, I have no idea how to get that info from the response stream...
So, how would I read the emails from the response (if I'm on the right track at all)?
Upvotes: 2
Views: 1276