コード例 #1
0
        public OperationDetails Create(UserDTO userDto)
        {
            ApplicationUser user = AppUserManager.FindByEmail(userDto.Email);

            if (user == null)
            {
                user = new ApplicationUser
                {
                    Email    = userDto.Email,
                    UserName = userDto.Email
                };
                var result = AppUserManager.Create(user, userDto.Password);
                if (result.Errors.Count() > 0)
                {
                    return(new OperationDetails(false, result.Errors.FirstOrDefault(), ""));
                }
                AppUserManager.AddToRoles(user.Id, userDto.Roles);
                ApplicationUserProfile clientProfile = new ApplicationUserProfile
                {
                    Id   = user.Id,
                    Name = userDto.Name
                };
                _database.UsersProfiles.Create(clientProfile);
                _database.Save();
                return(new OperationDetails(true, "Registration complete", ""));
            }
            else
            {
                return(new OperationDetails(false, "User with this login is already present", "Email"));
            }
        }
        public void PostConfigure(string name, OpenIdConnectOptions options)
        {
            options.Events = new OpenIdConnectEvents()
            {
                OnTicketReceived = async ticketReceivedContext =>
                {
                    var subject = ticketReceivedContext.Principal.Claims
                                  .FirstOrDefault(c => c.Type == "sub").Value;

                    var apiClient = _httpClientFactory.CreateClient("BasicAPIClient");

                    var request = new HttpRequestMessage(HttpMethod.Get, $"/api/applicationuserprofiles/{subject}");
                    request.SetBearerToken(ticketReceivedContext.Properties.GetTokenValue("access_token"));

                    var response = await apiClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);

                    response.EnsureSuccessStatusCode();

                    var applicationUserProfile = new ApplicationUserProfile();
                    using (var responseStream = await response.Content.ReadAsStreamAsync())
                    {
                        applicationUserProfile = await JsonSerializer.DeserializeAsync <ApplicationUserProfile>(responseStream);
                    }

                    var newClaimsIdentity = new ClaimsIdentity();
                    newClaimsIdentity.AddClaim(new Claim("subscriptionlevel", applicationUserProfile.SubscriptionLevel));

                    // add this additional identity
                    ticketReceivedContext.Principal.AddIdentity(newClaimsIdentity);
                }
            };
        }
コード例 #3
0
 private static UserProfileInfo MapUserProfile(ApplicationUserProfile profile)
 {
     return(new UserProfileInfo
     {
         Id = profile.User.DomainId,
         FirstName = profile.FirstName,
         LastName = profile.LastName
     });
 }
コード例 #4
0
ファイル: AccountController.cs プロジェクト: ediux/CRM
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Manage"));
            }

            if (ModelState.IsValid)
            {
                // 從外部登入提供者處取得使用者資訊
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new ApplicationUser {
                    UserName = info.DefaultUserName
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        user = UserManager.FindByName(model.Email);
                        if (user != null)
                        {
                            var profile = new ApplicationUserProfile()
                            {
                                EMail          = model.Email,
                                EMailConfirmed = true,
                                DisplayName    = model.Email
                            };
                            var setemailresult = await UserManager.SetEmailAsync(user.Id, model.Email);

                            if (setemailresult.Succeeded == false)
                            {
                                await UserManager.DeleteAsync(user);    //先標示刪除註冊者

                                ViewBag.ReturnUrl = returnUrl;
                                return(View("ExternalLoginFailure"));
                            }
                        }
                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
 public void AddApplicationUserProfile(ApplicationUserProfile applicationUserProfile)
 {
     _context.ApplicationUserProfiles.Add(applicationUserProfile);
 }