Ejemplo n.º 1
0
        public IActionResult Register(string returnUrl)
        {
            if (User.IsAuthenticated())
            {
                return(Redirect("~/"));
            }
            var model = new RegisterInputModel();

            model.ReturnUrl = returnUrl;
            // var bModel = JsonConvert.DeserializeObject<BasicRegisterViewModel>();

            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Register(RegisterInputModel model)
        {
            // check if we are in the context of an authorization request
            var context = await _interaction.GetAuthorizationContextAsync(model.ReturnUrl);

            if (ModelState.IsValid)
            {
                var user = await _identityService.UserManager.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    ModelState.AddModelError("Exist", "Email is already used");
                    return(View(model));
                }

                user = new User
                {
                    Email              = model.Email,
                    NormalizedEmail    = model.Email.ToUpper(),
                    UserName           = model.Email,
                    NormalizedUserName = model.Email.ToUpper(),
                    SecurityStamp      = Guid.NewGuid().ToString(),
                    HasPassword        = true
                };


                var claims = new List <Claim>()
                {
                    new Claim(ClaimTypes.GivenName, model.FirstName),
                    new Claim(ClaimTypes.Surname, model.LastName),
                };

                var umResult = await _identityService.UserManager.CreateAsync(user, model.Password);

                if (umResult.Succeeded)
                {
                    user = await _identityService.UserManager.FindByEmailAsync(user.Email);

                    await _identityService.UserManager.AddToRoleAsync(user, "user");

                    foreach (var c in claims)
                    {
                        await _identityService.UserManager.AddClaimAsync(user, c);
                    }
                    // return View("Login", new { returnUrl = model.ReturnUrl});
                }
                foreach (var error in umResult.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }


                var signInResult = await _signInManager.CheckPasswordSignInAsync(user, model.Password, false);

                if (!signInResult.IsLockedOut && signInResult.Succeeded)
                {
                    await _events.RaiseAsync(new UserLoginSuccessEvent(user.UserName, user.Id.ToString(), user.UserName));

                    // only set explicit expiration here if user chooses "remember me".
                    // otherwise we rely upon expiration configured in cookie middleware.
                    AuthenticationProperties props = null;
                    if (AccountOptions.AllowRememberLogin && model.RememberLogin)
                    {
                        props = new AuthenticationProperties
                        {
                            IsPersistent = true,
                            ExpiresUtc   = DateTimeOffset.UtcNow.Add(AccountOptions.RememberMeLoginDuration)
                        };
                    }
                    ;

                    // issue authentication cookie with subject ID and username
                    await HttpContext.SignInAsync(user.Id.ToString(), user.UserName, props);

                    if (context != null)
                    {
                        if (await _clientStore.IsPkceClientAsync(context.ClientId))
                        {
                            // if the client is PKCE then we assume it's native, so this change in how to
                            // return the response is for better UX for the end user.
                            return(View("Redirect", new RedirectViewModel {
                                RedirectUrl = model.ReturnUrl
                            }));
                        }

                        // we can trust model.ReturnUrl since GetAuthorizationContextAsync returned non-null
                        return(Redirect(model.ReturnUrl));
                    }

                    // request for a local page
                    if (Url.IsLocalUrl(model.ReturnUrl))
                    {
                        return(Redirect(model.ReturnUrl));
                    }
                    else if (string.IsNullOrEmpty(model.ReturnUrl))
                    {
                        return(Redirect("~/"));
                    }
                    else
                    {
                        // user might have clicked on a malicious link - should be logged
                        throw new Exception("invalid return URL");
                    }
                }
            }


            return(View(model));
        }