Exemple #1
0
        public async Task <IActionResult> ProcessAsync()
        {
            await _usersTable.CreateIfNotExistsAsync();

            try
            {
                Validator.ValidateRequest(_loginRequest);

                var openIDProvider = ResolveProvider();

                if (openIDProvider is null)
                {
                    return(new BadRequestObjectResult(new { errors = "Invalid state" }));
                }

                var openIDUserProfile = await openIDProvider.GetProfileAsync(_loginRequest.Token);

                if (openIDUserProfile is { })
                {
                    var normalizedEmail = openIDUserProfile.Email.ToUpperInvariant();

                    var existingUsers = _usersTable
                                        .CreateQuery <UserDto>()
                                        .AsQueryable()
                                        .Where(u => u.NormalizedEmail == normalizedEmail)
                                        .ToArray();

                    if (existingUsers.Any())
                    {
                        var user  = existingUsers[0];
                        var token = TokenManager.GenerateJSONWebToken(user);
                        return(new OkObjectResult(token));
                    }
                    else
                    {
                        var user = new UserDto
                        {
                            Email           = openIDUserProfile.Email,
                            NormalizedEmail = normalizedEmail,
                            FirstName       = openIDUserProfile.FirstName,
                            LastName        = openIDUserProfile.LastName,
                            PasswordHash    = string.Empty,
                            PasswordSalt    = string.Empty,
                            Timestamp       = DateTime.UtcNow,
                        };

                        await _usersTable.ExecuteAsync(TableOperation.Insert(user));

                        _logger.LogInformation("Created new user with id: {0}", user.RowKey);

                        var token = TokenManager.GenerateJSONWebToken(user);
                        return(new OkObjectResult(token));
                    }
                }

                return(new UnauthorizedObjectResult(new { errors = "Invalid email and/or password" }));
            }
Exemple #2
0
        public async Task <IActionResult> ProcessAsync()
        {
            await _usersTable.CreateIfNotExistsAsync();

            try
            {
                Validator.ValidateRequest(_registerRequest);

                var existingUsers = _usersTable
                                    .CreateQuery <UserDto>()
                                    .AsQueryable()
                                    .Where(u => u.Email == _registerRequest.Email)
                                    .ToArray();

                if (existingUsers.Any())
                {
                    return(new ConflictObjectResult(new { errors = "User with the same email already exists." }));
                }

                var hashPassword = CredentialManager.HashPassword(_registerRequest.Password);

                var user = new UserDto
                {
                    Email           = _registerRequest.Email,
                    NormalizedEmail = _registerRequest.Email.ToUpperInvariant(),
                    FirstName       = _registerRequest.FirstName,
                    LastName        = _registerRequest.LastName,
                    PasswordHash    = hashPassword.PasswordHash,
                    PasswordSalt    = hashPassword.PasswordSalt,
                    Timestamp       = DateTime.UtcNow,
                };

                await _usersTable.ExecuteAsync(TableOperation.Insert(user));

                _logger.LogInformation("Created new user with id: {0}", user.RowKey);

                return(new CreatedResult(string.Empty, new { id = user.RowKey }));
            }
            catch (ValidationException e)
            {
                return(new BadRequestObjectResult(new { errors = e.Message }));
            }
        }