63 lines
2.8 KiB
C#
63 lines
2.8 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNet.Authentication.OAuth;
|
|
using Microsoft.AspNet.Http.Authentication;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace Microsoft.AspNet.Authentication.MicrosoftAccount
|
|
{
|
|
internal class MicrosoftAccountAuthenticationHandler : OAuthAuthenticationHandler<MicrosoftAccountAuthenticationOptions>
|
|
{
|
|
public MicrosoftAccountAuthenticationHandler(HttpClient httpClient)
|
|
: base(httpClient)
|
|
{
|
|
}
|
|
|
|
protected override async Task<AuthenticationTicket> CreateTicketAsync(ClaimsIdentity identity, AuthenticationProperties properties, OAuthTokenResponse tokens)
|
|
{
|
|
var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
|
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
|
|
|
|
var response = await Backchannel.SendAsync(request, Context.RequestAborted);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
|
|
|
|
var context = new OAuthAuthenticatedContext(Context, Options, Backchannel, tokens, payload)
|
|
{
|
|
Properties = properties,
|
|
Principal = new ClaimsPrincipal(identity)
|
|
};
|
|
|
|
var identifier = MicrosoftAccountAuthenticationHelper.GetId(payload);
|
|
if (!string.IsNullOrEmpty(identifier))
|
|
{
|
|
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, identifier, ClaimValueTypes.String, Options.ClaimsIssuer));
|
|
identity.AddClaim(new Claim("urn:microsoftaccount:id", identifier, ClaimValueTypes.String, Options.ClaimsIssuer));
|
|
}
|
|
|
|
var name = MicrosoftAccountAuthenticationHelper.GetName(payload);
|
|
if (!string.IsNullOrEmpty(name))
|
|
{
|
|
identity.AddClaim(new Claim(ClaimTypes.Name, name, ClaimValueTypes.String, Options.ClaimsIssuer));
|
|
identity.AddClaim(new Claim("urn:microsoftaccount:name", name, ClaimValueTypes.String, Options.ClaimsIssuer));
|
|
}
|
|
|
|
var email = MicrosoftAccountAuthenticationHelper.GetEmail(payload);
|
|
if (!string.IsNullOrEmpty(email))
|
|
{
|
|
identity.AddClaim(new Claim(ClaimTypes.Email, email, ClaimValueTypes.String, Options.ClaimsIssuer));
|
|
}
|
|
|
|
await Options.Events.Authenticated(context);
|
|
|
|
return new AuthenticationTicket(context.Principal, context.Properties, context.Options.AuthenticationScheme);
|
|
}
|
|
}
|
|
}
|