-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserProfileUtils.cs
105 lines (95 loc) · 3.75 KB
/
UserProfileUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace MailService.OAuthOutlook
{
internal static class UserProfileUtils
{
const string LiveGetProfileUri = "https://apis.live.net/v5.0/me?access_token=";
protected class ExtendedMicrosoftClientUserData
{
public string FirstName { get; set; }
public string Gender { get; set; }
public string Id { get; set; }
public string LastName { get; set; }
public Uri Link { get; set; }
public string Name { get; set; }
public Emails Emails { get; set; }
}
protected class Emails
{
public string Preferred { get; set; }
public string Account { get; set; }
public string Personal { get; set; }
public string Business { get; set; }
}
private static readonly string[] UriRfc3986CharsToEscape = new string[] { "!", "*", "'", "(", ")" };
private static string EscapeUriDataStringRfc3986(string value)
{
StringBuilder escaped = new StringBuilder(Uri.EscapeDataString(value));
// Upgrade the escaping to RFC 3986, if necessary.
for (int i = 0; i < UriRfc3986CharsToEscape.Length; i++)
{
escaped.Replace(UriRfc3986CharsToEscape[i], Uri.HexEscape(UriRfc3986CharsToEscape[i][0]));
}
// Return the fully-RFC3986-escaped string.
return escaped.ToString();
}
// Inspired by http://answer.techwikihow.com/154458/getting-email-oauth-authentication-microsoft.html
// Be sure to have "wl.emails" in the requested scopes if you're using this method.
public static async Task<IDictionary<string, string>> GetUserDataAsync(string accessToken)
{
ExtendedMicrosoftClientUserData graph;
WebRequest request =
WebRequest.Create(LiveGetProfileUri + EscapeUriDataStringRfc3986(accessToken));
WebResponse response = null;
try
{
try
{
response = (WebResponse)await request.GetResponseAsync();
}
catch (WebException wex)
{
if (wex.Status == WebExceptionStatus.ProtocolError)
{
await ExceptionUtils.ThrowWithBodyAsync(wex);
}
else
{
throw;
}
}
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(responseStream))
{
string data = await sr.ReadToEndAsync();
graph = JsonConvert.DeserializeObject<ExtendedMicrosoftClientUserData>(data);
}
}
}
finally
{
if (response != null)
{
response.Close();
}
}
Dictionary<string, string> userData = new Dictionary<string, string>();
userData.Add("id", graph.Id);
userData.Add("username", graph.Name);
userData.Add("name", graph.Name);
userData.Add("link", graph.Link == null ? null : graph.Link.AbsoluteUri);
userData.Add("gender", graph.Gender);
userData.Add("firstname", graph.FirstName);
userData.Add("lastname", graph.LastName);
userData.Add("email", graph.Emails.Preferred);
return userData;
}
}
}