public async Task <ActionResult> RegisterUser(User user)
        {
            try
            {
                if (user == null)
                {
                    logger.LogDebug($"Response <{nameof(BadRequest)}>, given user is null");
                    return(BadRequest("User not specified"));
                }

                var userExist = await userDataModel.GetUser(user.Username).ConfigureAwait(true);

                if (userExist == null)
                {
                    user.Password = user.Password.ToSha256();
                    await userDataModel.InsertUser(user).ConfigureAwait(true);

                    logger.LogDebug($"Response <{nameof(Ok)}>, User with username <{user.Username}> created");
                    return(Ok("User created"));
                }

                logger.LogDebug($"Response <{nameof(Conflict)}>>, User with username <{user.Username}> already exists");
                return(Conflict("User already exist"));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, GetType().Name);
                throw;
            }
        }
예제 #2
0
        // Get user profile date in terms of claims when calling /connect/userinfo
        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            try
            {
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

                // depending on the scope accessing the user data.
                if (!string.IsNullOrEmpty(context.Subject.Identity.Name))
                {
                    // get user from db (in my case this is by username)
                    var user = await userDataModel.GetUser(context.Subject.Identity.Name).ConfigureAwait(true);

                    if (user != null)
                    {
                        var claims = new List <Claim>
                        {
                            new Claim(JwtClaimTypes.PreferredUserName, user.Username ?? string.Empty),
                        };

                        // set issued claims to return
                        context.IssuedClaims = claims.Where(x => context.RequestedClaimTypes.Contains(x.Type)).ToList();
                    }
                }
                else
                {
                    // get subject from context (this was set ResourceOwnerPasswordValidator.ValidateAsync),
                    var username = context.Subject.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.PreferredUserName).Value;

                    if (!string.IsNullOrEmpty(username))
                    {
                        // get user from db (find user by username)
                        var user = await userDataModel.GetUser(username).ConfigureAwait(true);

                        // issue the claims for the user
                        if (user != null)
                        {
                            var claims = new List <Claim>
                            {
                                new Claim(JwtClaimTypes.PreferredUserName, user.Username ?? string.Empty),
                            };

                            context.IssuedClaims = claims.ToList();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, GetType().Name);
                throw;
            }
        }
        public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
        {
            try
            {
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

                var user = await userDataModel.GetUser(context.UserName).ConfigureAwait(true);

                if (user != null)
                {
                    if (user.Password == context.Password)
                    {
                        var claims = new List <Claim>
                        {
                            new Claim(JwtClaimTypes.PreferredUserName, user.Username ?? string.Empty),
                        };

                        // set the result
                        context.Result = new GrantValidationResult(
                            subject: user.Username,
                            authenticationMethod: "custom",
                            claims: claims);

                        return;
                    }

                    context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "Incorrect password");
                    return;
                }
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "User does not exist.");
            }
            catch (Exception ex)
            {
                logger.LogError(ex, GetType().Name);
                throw;
            }
        }