diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2012-03-01 22:55:03 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2012-03-01 23:19:26 -0800 |
commit | 198bffe042a3650095b27bed29d0f8c98bc5c926 (patch) | |
tree | bc1b2178b73d4303221ac48d320c758751abe5e9 /src/DotNetOpenAuth.AspNet/Clients/OAuth2/WindowsLiveClient.cs | |
parent | 6bc4c6db7529501e8a2c0b7fa54a24fb8e4dbf42 (diff) | |
download | DotNetOpenAuth-198bffe042a3650095b27bed29d0f8c98bc5c926.zip DotNetOpenAuth-198bffe042a3650095b27bed29d0f8c98bc5c926.tar.gz DotNetOpenAuth-198bffe042a3650095b27bed29d0f8c98bc5c926.tar.bz2 |
ReSharper code cleanup to help get this AspNet contribution into StyleCop compliance.
Diffstat (limited to 'src/DotNetOpenAuth.AspNet/Clients/OAuth2/WindowsLiveClient.cs')
-rw-r--r-- | src/DotNetOpenAuth.AspNet/Clients/OAuth2/WindowsLiveClient.cs | 150 |
1 files changed, 102 insertions, 48 deletions
diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth2/WindowsLiveClient.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth2/WindowsLiveClient.cs index 548d6bd..dced87f 100644 --- a/src/DotNetOpenAuth.AspNet/Clients/OAuth2/WindowsLiveClient.cs +++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth2/WindowsLiveClient.cs @@ -9,60 +9,140 @@ namespace DotNetOpenAuth.AspNet.Clients { using System.Collections.Generic; using System.IO; using System.Net; - using System.Text; using DotNetOpenAuth.Messaging; + /// <summary> + /// The windows live client. + /// </summary> public sealed class WindowsLiveClient : OAuth2Client { - private const string TokenEndpoint = "https://oauth.live.com/token"; + #region Constants and Fields + + /// <summary> + /// The authorization endpoint. + /// </summary> private const string AuthorizationEndpoint = "https://oauth.live.com/authorize"; + + /// <summary> + /// The token endpoint. + /// </summary> + private const string TokenEndpoint = "https://oauth.live.com/token"; + + /// <summary> + /// The _app id. + /// </summary> private readonly string _appId; + + /// <summary> + /// The _app secret. + /// </summary> private readonly string _appSecret; + #endregion + + #region Constructors and Destructors + + /// <summary> + /// Initializes a new instance of the <see cref="WindowsLiveClient"/> class. + /// </summary> + /// <param name="appId"> + /// The app id. + /// </param> + /// <param name="appSecret"> + /// The app secret. + /// </param> + /// <exception cref="ArgumentNullException"> + /// </exception> + /// <exception cref="ArgumentNullException"> + /// </exception> public WindowsLiveClient(string appId, string appSecret) : base("windowslive") { - if (String.IsNullOrEmpty(appId)) { + if (string.IsNullOrEmpty(appId)) { throw new ArgumentNullException("appId"); } - if (String.IsNullOrEmpty("appSecret")) { + if (string.IsNullOrEmpty("appSecret")) { throw new ArgumentNullException("appSecret"); } - _appId = appId; - _appSecret = appSecret; + this._appId = appId; + this._appSecret = appSecret; } + #endregion + + #region Methods + /// <summary> - /// Gets the full url pointing to the login page for this client. The url should include the - /// specified return url so that when the login completes, user is redirected back to that url. + /// Gets the full url pointing to the login page for this client. The url should include the specified return url so that when the login completes, user is redirected back to that url. /// </summary> - /// <param name="returnUrl">The return URL.</param> + /// <param name="returnUrl"> + /// The return URL. + /// </param> protected override Uri GetServiceLoginUrl(Uri returnUrl) { var builder = new UriBuilder(AuthorizationEndpoint); - builder.AppendQueryArgs(new Dictionary<string, string> { - { "client_id", _appId }, - { "scope", "wl.basic" }, - { "response_type", "code" }, - { "redirect_uri", returnUrl.AbsoluteUri }, + builder.AppendQueryArgs( + new Dictionary<string, string> { + { "client_id", this._appId }, + { "scope", "wl.basic" }, + { "response_type", "code" }, + { "redirect_uri", returnUrl.AbsoluteUri }, }); return builder.Uri; } /// <summary> + /// Given the access token, gets the logged-in user's data. The returned dictionary must include two keys 'id', and 'username'. + /// </summary> + /// <param name="accessToken"> + /// The access token of the current user. + /// </param> + /// <returns> + /// A dictionary contains key-value pairs of user data + /// </returns> + protected override IDictionary<string, string> GetUserData(string accessToken) { + WindowsLiveUserData graph; + var request = + WebRequest.Create( + "https://apis.live.net/v5.0/me?access_token=" + MessagingUtilities.EscapeUriDataStringRfc3986(accessToken)); + using (var response = request.GetResponse()) { + using (var responseStream = response.GetResponseStream()) { + graph = JsonHelper.Deserialize<WindowsLiveUserData>(responseStream); + } + } + + var userData = new Dictionary<string, string>(); + userData.AddItemIfNotEmpty("id", graph.Id); + userData.AddItemIfNotEmpty("username", graph.Name); + userData.AddItemIfNotEmpty("name", graph.Name); + userData.AddItemIfNotEmpty("link", graph.Link == null ? null : graph.Link.AbsoluteUri); + userData.AddItemIfNotEmpty("gender", graph.Gender); + userData.AddItemIfNotEmpty("firstname", graph.FirstName); + userData.AddItemIfNotEmpty("lastname", graph.LastName); + return userData; + } + + /// <summary> /// Queries the access token from the specified authorization code. /// </summary> - /// <param name="returnUrl">The return URL.</param> - /// <param name="authorizationCode">The authorization code.</param> + /// <param name="returnUrl"> + /// The return URL. + /// </param> + /// <param name="authorizationCode"> + /// The authorization code. + /// </param> + /// <returns> + /// The query access token. + /// </returns> protected override string QueryAccessToken(Uri returnUrl, string authorizationCode) { var entity = MessagingUtilities.CreateQueryString( new Dictionary<string, string> { - { "client_id", _appId }, - { "redirect_uri", returnUrl.AbsoluteUri }, - { "client_secret", _appSecret }, - { "code", authorizationCode }, - { "grant_type", "authorization_code" }, + { "client_id", this._appId }, + { "redirect_uri", returnUrl.AbsoluteUri }, + { "client_secret", this._appSecret }, + { "code", authorizationCode }, + { "grant_type", "authorization_code" }, }); WebRequest tokenRequest = WebRequest.Create(TokenEndpoint); @@ -89,32 +169,6 @@ namespace DotNetOpenAuth.AspNet.Clients { return null; } - /// <summary> - /// Given the access token, gets the logged-in user's data. The returned dictionary must include - /// two keys 'id', and 'username'. - /// </summary> - /// <param name="accessToken">The access token of the current user.</param> - /// <returns> - /// A dictionary contains key-value pairs of user data - /// </returns> - protected override IDictionary<string, string> GetUserData(string accessToken) { - WindowsLiveUserData graph; - var request = WebRequest.Create("https://apis.live.net/v5.0/me?access_token=" + MessagingUtilities.EscapeUriDataStringRfc3986(accessToken)); - using (var response = request.GetResponse()) { - using (var responseStream = response.GetResponseStream()) { - graph = JsonHelper.Deserialize<WindowsLiveUserData>(responseStream); - } - } - - var userData = new Dictionary<string, string>(); - userData.AddItemIfNotEmpty("id", graph.Id); - userData.AddItemIfNotEmpty("username", graph.Name); - userData.AddItemIfNotEmpty("name", graph.Name); - userData.AddItemIfNotEmpty("link", graph.Link == null ? null : graph.Link.AbsoluteUri); - userData.AddItemIfNotEmpty("gender", graph.Gender); - userData.AddItemIfNotEmpty("firstname", graph.FirstName); - userData.AddItemIfNotEmpty("lastname", graph.LastName); - return userData; - } + #endregion } } |