コード例 #1
0
        public UserEntity AddUser(SignupUserContract user, out AuthInfo authInfo)
        {
            if (!string.IsNullOrEmpty(user.Email.Trim()) && EmailExistsAsync(user.Email).Result)
            {
                authInfo = null;
                return(null);
            }

            var newId        = Guid.NewGuid();
            var expiration   = DateTime.UtcNow.AddDays(ExpirationDays);
            var bearerToken  = TokenGenerator.GenerateToken(newId, _configuration["Security:SecretKey"], expiration, null, _configuration["Security:EncryptionKey"]);
            var passwordHash = CredentialUtility.HashPassword(user.Password);

            var addUser = new UserEntity
            {
                UserId         = newId,
                Email          = user.Email,
                EmailCandidate = user.Email,
                EmailConfirmed = false,
                PasswordHash   = passwordHash,
                BearerToken    = bearerToken,
                DateJoined     = DateTimeOffset.UtcNow
            };

            authInfo = new AuthInfo
            {
                Token      = bearerToken,
                Expiration = expiration
            };

            _userRepository.AddAsync(addUser).Wait();
            return(addUser);
        }
コード例 #2
0
        public async Task <AuthInfo> LoginUserEmailAsync(LoginUserContract user)
        {
            if (string.IsNullOrEmpty(user.Email) || string.IsNullOrEmpty(user.Email.Trim()))
            {
                return(null);
            }
            var foundUser = await _userRepository.GetAsync(a => a.Email == user.Email.Trim());

            if (foundUser == null)
            {
                return(null);
            }

            if (CredentialUtility.IsValidPassword(foundUser, user.Password))
            {
                var expiration  = DateTime.UtcNow.AddDays(ExpirationDays);
                var bearerToken = TokenGenerator.GenerateToken(foundUser.UserId, _configuration["Security:SecretKey"], expiration);
                foundUser.BearerToken = bearerToken;
                await _userRepository.UpdateAsync(a => a.UserId == foundUser.UserId, foundUser);

                return(new AuthInfo
                {
                    Token = bearerToken,
                    Expiration = expiration
                });
            }
            return(null);
        }
コード例 #3
0
        public ActionResult <object> GenerateChallenge(string name)
        {
            var rpid      = RelyingPartyId;
            var challenge = CredentialUtility.CreateChallenge();

            HttpContext.Session.Set("name", Encoding.UTF8.GetBytes(name));
            HttpContext.Session.Set("challenge", challenge);

            return(new
            {
                relyingPartyId = rpid,
                relyingParty = rpid,
                challenge
            });
        }