示例#1
0
        public static async Task PostProfileForm(HttpContext context)
        {
            var principal      = (ClaimsPrincipal)context.User;
            var idClaim        = principal.FindFirst(nameIdentifierClaimType);
            var formData       = context.Request.Form;
            var updatedProfile = new UserProfile()
            {
                Id       = idClaim.Value,
                Etag     = formData["etag"],
                FullName = formData["fullName"],
                Email    = formData["email"],
            };

            var pictures = new Pictures(context);

            // check for picture and upload to blob store
            if (formData.Files.Any())
            {
                System.Console.WriteLine($"files in form: {formData.Files.Count}");
                var stream     = formData.Files.First().OpenReadStream();
                var pictureUrl = await pictures.PutPicture(updatedProfile.Id, stream);
            }
            updatedProfile.PictureUrl = await pictures.GetPictureUrl(updatedProfile.Id);

            var profiles = new UserProfiles();
            await profiles.UpdateProfileAsync(updatedProfile);

            await RenderProfilePage(context);

            return;
        }
示例#2
0
        public static async Task <UserProfile> GetUserProfileFromContext(HttpContext context)
        {
            var logger = context.RequestServices.GetRequiredService <ILogger <IWebHost> >();

            if (!context.User.Identity.IsAuthenticated)
            {
                await context.ChallengeAsync();

                return(new UserProfile());
            }

            ClaimsPrincipal principal = (ClaimsPrincipal)context.User;
            var             idClaim   = principal.FindFirst(nameIdentifierClaimType);
            var             nameClaim = principal.FindFirst(nameClaimType);

            logger.LogInformation($"[{nameClaim}, {idClaim}]");
            foreach (Claim claim in principal.Claims)
            {
                logger.LogInformation($"claim: [{claim}]");
            }

            var profiles = new UserProfiles();
            // first see if profile exists for this ID
            var profile = await profiles.GetProfileAsync(idClaim.Value);

            // if not, add a new profile for this ID
            if (profile == null)
            {
                await profiles.AddProfileAsync(new UserProfile()
                {
                    Id                 = idClaim.Value,
                    FullName           = principal.FindFirst(nameClaimType).Value,
                    Email              = principal.FindFirst(emailClaimType).Value,
                    ExternalIdentifier = idClaim.Value
                });

                profile = await profiles.GetProfileAsync(idClaim.Value);
            }
            ;

            logger.LogInformation($"got profileId {profile.Id}, fullName {profile.FullName}");
            return(profile);
        }