//-----------------------------------------------------------------------
//
// Copyright (c) Microsoft. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.AspNet.Clients {
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using DotNetOpenAuth.Messaging;
///
/// The windows live client.
///
public sealed class WindowsLiveClient : OAuth2Client {
#region Constants and Fields
///
/// The authorization endpoint.
///
private const string AuthorizationEndpoint = "https://oauth.live.com/authorize";
///
/// The token endpoint.
///
private const string TokenEndpoint = "https://oauth.live.com/token";
///
/// The _app id.
///
private readonly string appId;
///
/// The _app secret.
///
private readonly string appSecret;
#endregion
#region Constructors and Destructors
///
/// Initializes a new instance of the class.
///
///
/// The app id.
///
///
/// The app secret.
///
public WindowsLiveClient(string appId, string appSecret)
: base("windowslive") {
Requires.NotNullOrEmpty(appId, "appId");
Requires.NotNullOrEmpty(appSecret, "appSecret");
this.appId = appId;
this.appSecret = appSecret;
}
#endregion
#region Methods
///
/// 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.
///
/// The return URL.
///
/// An absolute URL.
///
protected override Uri GetServiceLoginUrl(Uri returnUrl) {
var builder = new UriBuilder(AuthorizationEndpoint);
builder.AppendQueryArgs(
new Dictionary {
{ "client_id", this.appId },
{ "scope", "wl.basic" },
{ "response_type", "code" },
{ "redirect_uri", returnUrl.AbsoluteUri },
});
return builder.Uri;
}
///
/// Given the access token, gets the logged-in user's data. The returned dictionary must include two keys 'id', and 'username'.
///
///
/// The access token of the current user.
///
///
/// A dictionary contains key-value pairs of user data
///
protected override IDictionary 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(responseStream);
}
}
var userData = new Dictionary();
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;
}
///
/// Queries the access token from the specified authorization code.
///
///
/// The return URL.
///
///
/// The authorization code.
///
///
/// The query access token.
///
protected override string QueryAccessToken(Uri returnUrl, string authorizationCode) {
var entity =
MessagingUtilities.CreateQueryString(
new Dictionary {
{ "client_id", this.appId },
{ "redirect_uri", returnUrl.AbsoluteUri },
{ "client_secret", this.appSecret },
{ "code", authorizationCode },
{ "grant_type", "authorization_code" },
});
WebRequest tokenRequest = WebRequest.Create(TokenEndpoint);
tokenRequest.ContentType = "application/x-www-form-urlencoded";
tokenRequest.ContentLength = entity.Length;
tokenRequest.Method = "POST";
using (Stream requestStream = tokenRequest.GetRequestStream()) {
var writer = new StreamWriter(requestStream);
writer.Write(entity);
writer.Flush();
}
HttpWebResponse tokenResponse = (HttpWebResponse)tokenRequest.GetResponse();
if (tokenResponse.StatusCode == HttpStatusCode.OK) {
using (Stream responseStream = tokenResponse.GetResponseStream()) {
var tokenData = JsonHelper.Deserialize(responseStream);
if (tokenData != null) {
return tokenData.AccessToken;
}
}
}
return null;
}
#endregion
}
}