Example #1
0
        public IActionResult OnGet(int id)
        {
            Account = _accountRepo.GetById(id);
            Profile = _profileRepo.GetByAccountId(id);
            if (Account == null || Profile == null)
            {
                return(NotFound());
            }

            UserRoles      = _roleRepo.GetAllFormAccountId(id);
            AvailableRoles = _roleRepo.GetAll()
                             .Except(UserRoles)
                             .Select(r => new SelectListItem(r.Name, r.Id.ToString()));

            Input = new InputModel {
                DateOfBirth   = Profile.Birthday,
                FirstName     = Profile.FirstName,
                LastName      = Profile.LastName,
                PerferredName = Profile.PreferredName,
                Phone         = Profile.Phone,
                Email         = Account.EMail,
                Status        = Account.Status
            };
            return(Page());
        }
Example #2
0
        public async Task SignInAsync(string email, bool rememberMe, HttpContext httpContext)
        {
            var account = _accountRepo.GetByEmail(email);

            if (account == null)
            {
                var msg = $"email: {email} does not exist in accounts";
                throw new ArgumentException(msg, nameof(email));
            }

            var profile = _profileRepo.GetByAccountId(account.Id);
            var roles   = _roleRepo.GetAllFormAccountId(account.Id);

            var claims = new List <Claim> {
                new Claim(ClaimTypes.Email, account.EMail),
                new Claim(ClaimTypes.NameIdentifier, account.Id.ToString()),
                new Claim(ClaimTypes.GivenName, profile.FirstName),
                new Claim(ClaimTypes.Surname, profile.LastName),
                new Claim(ClaimTypes.Name, profile.PreferredFirstLastName),
            };

            if (profile.Birthday.HasValue)
            {
                claims.Add(new Claim(ClaimTypes.DateOfBirth, profile.Birthday.Value.ToLongDateString()));
            }

            if (!string.IsNullOrWhiteSpace(profile.Phone))
            {
                claims.Add(new Claim(ClaimTypes.OtherPhone, profile.Phone));
            }

            claims.AddRange(roles.Select(r => new Claim(ClaimTypes.Role, r.Name)));

            var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

            var authProperties = new AuthenticationProperties {
                AllowRefresh = true,
                ExpiresUtc   = rememberMe ? DateTimeOffset.UtcNow.AddMonths(1) : DateTimeOffset.UtcNow.AddDays(1),
                IsPersistent = true,
                IssuedUtc    = DateTime.UtcNow
            };

            await httpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(claimsIdentity),
                authProperties);

            account.LastLoginAt = DateTime.UtcNow;
            _accountRepo.Update(account);
        }