Ejemplo n.º 1
0
        protected override async Task <AuthenticationTicket> CreateTicketAsync(
            ClaimsIdentity identity,
            AuthenticationProperties properties,
            OAuthTokenResponse tokens)
        {
            using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);

            using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);

            if (!response.IsSuccessStatusCode)
            {
                Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
                                "returned a {Status} response with the following payload: {Headers} {Body}.",
                                response.StatusCode,
                                response.Headers.ToString(),
                                await response.Content.ReadAsStringAsync());

                throw new HttpRequestException("An error occurred while retrieving the user profile.");
            }

            using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
            var principal = new ClaimsPrincipal(identity);
            var context   = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);

            context.RunClaimActions();

            await Options.Events.CreatingTicket(context);

            // store token and user in database
            string id           = "";
            string emailAddress = "";
            string name         = "";

            foreach (Claim claim in identity.Claims)
            {
                switch (claim.Type)
                {
                case "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier":
                    id = claim.Value;
                    break;

                case "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress":
                    emailAddress = claim.Value;
                    break;

                case "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name":
                    name = claim.Value;
                    break;
                }
            }
            CommentLinkUser commentLinkUser = new CommentLinkUser(id, emailAddress, name, tokens);

            _ = CosmosDb.UpsertItem(commentLinkUser);
            return(new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name));
        }
Ejemplo n.º 2
0
 public async Task UpsertItem(CommentLinkUser commentLinkUser)
 {
     await _container.UpsertItemAsync(commentLinkUser, new PartitionKey(commentLinkUser.Id));
 }