Exemplo n.º 1
0
        protected override async Task <AuthenticationTicket> GetUserInformationAsync(AuthenticationProperties properties, TokenResponse tokens)
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
            HttpResponseMessage graphResponse = await Backchannel.SendAsync(request, Context.RequestAborted);

            graphResponse.EnsureSuccessStatusCode();
            string accountString = await graphResponse.Content.ReadAsStringAsync();

            JObject accountInformation = JObject.Parse(accountString);

            var context = new MicrosoftAccountAuthenticatedContext(Context, Options, accountInformation, tokens);

            context.Properties = properties;
            context.Identity   = new ClaimsIdentity(
                new[]
            {
                new Claim(ClaimTypes.NameIdentifier, context.Id, ClaimValueTypes.String, Options.AuthenticationType),
                new Claim(ClaimTypes.Name, context.Name, ClaimValueTypes.String, Options.AuthenticationType),
                new Claim("urn:microsoftaccount:id", context.Id, ClaimValueTypes.String, Options.AuthenticationType),
                new Claim("urn:microsoftaccount:name", context.Name, ClaimValueTypes.String, Options.AuthenticationType)
            },
                Options.AuthenticationType,
                ClaimsIdentity.DefaultNameClaimType,
                ClaimsIdentity.DefaultRoleClaimType);

            if (!string.IsNullOrWhiteSpace(context.Email))
            {
                context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Email, ClaimValueTypes.String, Options.AuthenticationType));
            }

            await Options.Notifications.Authenticated(context);

            return(new AuthenticationTicket(context.Identity, context.Properties));
        }
 /// <summary>
 /// Invoked whenever Microsoft succesfully authenticates a user
 /// </summary>
 /// <param name="context">Contains information about the login session as well as the user <see cref="System.Security.Claims.ClaimsIdentity"/>.</param>
 /// <returns>A <see cref="Task"/> representing the completed operation.</returns>
 public virtual Task Authenticated(MicrosoftAccountAuthenticatedContext context)
 {
     return(OnAuthenticated(context));
 }