Created
June 22, 2012 14:15
-
-
Save matejskubic/2972977 to your computer and use it in GitHub Desktop.
Windows Live Sts That Returns Email, Name Claims
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.IO; | |
using System.Web.Mvc; | |
using System.Runtime.Serialization; | |
using System.Runtime.Serialization.Json; | |
using Microsoft.IdentityModel.Protocols.WSFederation; | |
using Microsoft.IdentityModel.SecurityTokenService; | |
using Microsoft.IdentityModel.Web; | |
using Web.Core; | |
namespace Web.LiveSts.Controllers | |
{ | |
[DataContract] | |
public class AccessTokenResult : BaseJsonResult | |
{ | |
[DataMember(Name = "access_token")] | |
public string AccessToken { get; set; } | |
[DataMember(Name = "expires_in")] | |
public int ExpiresIn { get; set; } | |
[DataMember(Name = "scope")] | |
public string Scope { get; set; } | |
[DataMember(Name = "token_type")] | |
public string TokenType { get; set; } | |
} | |
[DataContract] | |
public class MeResult : BaseJsonResult | |
{ | |
[DataMember(Name = "id")] | |
public string Id { get; set; } | |
[DataMember(Name = "name")] | |
public string Name { get; set; } | |
[DataMember(Name = "emails")] | |
public EmailResult Emails { get; set; } | |
} | |
[DataContract] | |
public class EmailResult : BaseJsonResult | |
{ | |
[DataMember(Name = "preferred")] | |
public string Preferred { get; set; } | |
[DataMember(Name = "account")] | |
public string Account { get; set; } | |
[DataMember(Name = "personal")] | |
public string Personal { get; set; } | |
[DataMember(Name = "business")] | |
public string Business { get; set; } | |
} | |
public class HomeController : Controller | |
{ | |
public ActionResult Index(string code) | |
{ | |
string action = Request.QueryString[WSFederationConstants.Parameters.Action]; | |
string domainUrl = Url.AbsoluteAction(string.Empty, string.Empty); | |
if (action == WSFederationConstants.Actions.SignIn && !string.IsNullOrEmpty(code)) | |
{ | |
System.Net.WebClient client = new System.Net.WebClient(); | |
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); | |
string response = client.UploadString("https://oauth.live.com/token", string.Format("client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&grant_type=authorization_code", | |
System.Configuration.ConfigurationManager.AppSettings["WindowsLiveClientIdFor-" + domainUrl], | |
Server.UrlEncode(Request.Url.OriginalString.Replace("&code=" + code, string.Empty)), | |
Server.UrlEncode(System.Configuration.ConfigurationManager.AppSettings["WindowsLiveClientSecretFor-" + domainUrl]), | |
code)); | |
AccessTokenResult auth = new JsonProvider<AccessTokenResult>().GetResult(response); | |
MeResult profile = new JsonProvider<MeResult>().GetResult(new System.Net.WebClient().DownloadString("https://apis.live.net/v5.0/me?access_token=" + auth.AccessToken)); | |
SignInRequestMessage requestMessage = (SignInRequestMessage)WSFederationMessage.CreateFromUri(Request.Url); | |
if (profile != null) | |
{ | |
Microsoft.IdentityModel.SecurityTokenService.SecurityTokenService sts = new CustomSecurityTokenService(CustomSecurityTokenServiceConfiguration.Current(Url.AbsoluteAction("", "")), profile); | |
SignInResponseMessage responseMessage = FederatedPassiveSecurityTokenServiceOperations.ProcessSignInRequest(requestMessage, User, sts); | |
return new ContentResult | |
{ | |
Content = responseMessage.WriteFormPost() | |
}; | |
} | |
else | |
{ | |
throw new UnauthorizedAccessException(); | |
} | |
} | |
else if (action == WSFederationConstants.Actions.SignOut) | |
{ | |
SignOutRequestMessage signoutMessage = (SignOutRequestMessage)WSFederationMessage.CreateFromUri(Request.Url); | |
FederatedAuthentication.SessionAuthenticationModule.SignOut(); | |
if (!string.IsNullOrWhiteSpace(signoutMessage.Reply)) | |
{ | |
return Redirect(signoutMessage.Reply); | |
} | |
System.Web.Security.FormsAuthentication.SignOut(); | |
} | |
if (string.IsNullOrEmpty(code)) | |
return Redirect(string.Format("https://oauth.live.com/authorize?client_id={0}&scope=wl.signin%20wl.emails&response_type=code&redirect_uri={1}", | |
System.Configuration.ConfigurationManager.AppSettings["WindowsLiveClientIdFor-" + domainUrl], | |
Server.UrlEncode(Request.Url.OriginalString))); | |
return Content(""); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment