Пример #1
0
        private async Task Authenticate(DomainUser user)
        {
            var claims = new List <Claim>
            {
                new Claim(ClaimsIdentity.DefaultNameClaimType, user.Email),
                new Claim(ClaimsIdentity.DefaultRoleClaimType, user.Role.ToString()),
            };

            ClaimsIdentity id = new ClaimsIdentity(claims, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType,
                                                   ClaimsIdentity.DefaultRoleClaimType);

            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id));
        }
Пример #2
0
        public async Task <IActionResult> Register(RegisterModel model)
        {
            if (!User.Identity.IsAuthenticated)
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        DomainUser domainUser = await _userManager.CreateUserAsync(model.Email, model.Password, UserRoles.User); // token was generated automatically

                        string subject     = "Test System";
                        string senderName  = "Test System Administration";
                        var    callbackUrl = Url.Action("ConfirmEmail", "Account", new
                        {
                            @UserId            = domainUser.Id,
                            @ConfirmationToken = domainUser.ConfirmationToken.ToString()
                        },
                                                        protocol: HttpContext.Request.Scheme);

                        string messgae = $"Confirm your account by clicking: <a href='{callbackUrl}'>link</a>";

                        _emailService.SendEmailAsync(senderName, WebExtensions.SenderEmail, WebExtensions.SenderEmailPassword, WebExtensions.SmtpHost,
                                                     WebExtensions.SmtpPort, model.Email, subject, messgae); // not awaiting

                        return(RedirectToAction("ConfirmEmailPage", "Account", new { @userId = domainUser.Id }));
                    }
                    catch (UserAlreadyExistsException)
                    {
                        ModelState.AddModelError("", "User with this email already exists");

                        return(View(model));
                    }
                }

                ModelState.AddModelError("", "Invalid Login or(and) Password");

                return(View(model));
            }

            return(RedirectToAction("Index", "Home"));
        }