Example #1
0
        public async Task <ActionResult <LoginResponseModel> > Login([FromBody] LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userLookup = await UserManager.FindUserByUsernameOrEmail(model.UsernameOrEmail);

            if (!userLookup.Found)
            {
                return(BadRequest(new { message = BadLoginMessage }));
            }

            var signInResult = await SignInManager.PasswordSignInAsync(userLookup.Value, model.Password, false, true);

            if (signInResult.IsLockedOut)
            {
                return(BadRequest(new { message = UserLockedOutMessage }));
            }

            if (!signInResult.Succeeded)
            {
                return(BadRequest(new { message = BadLoginMessage }));
            }

            //var jwt = JwtProducer.Generate(userLookup.Value);

            return(Ok(new LoginResponseModel
            {
                UserName = userLookup.Value.UserName,
                Email = userLookup.Value.Email,
                Jwt = JwtProducer.Generate(userLookup.Value)
            }));
        }
Example #2
0
        public async Task <ActionResult <LoginResponseModel> > RegisterNewUser(
            [FromBody] RegisterNewUserModel model,
            [FromHeader(Name = Headers.AdminKey)] string adminKey)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await UserManager.FindUserByUsernameOrEmail(model.Email);

            if (user.Found)
            {
                return(Conflict(new { message = CredentialsAlreadyTaken }));
            }

            var newUser = Mapper.Map <DbUser>(model);

            if ((UserConfig.DefaultUserClaims != null) && (UserConfig.DefaultUserClaims.Any()))
            {
                var defClaims = UserConfig.DefaultUserClaims
                                .Split(';')
                                .Select(c => c.Split('='));

                newUser.Claims ??= new Collection <DbUserClaim>();

                foreach (var defClaim in defClaims)
                {
                    newUser.Claims.Add(new DbUserClaim
                    {
                        ClaimType  = defClaim.First().Trim(),
                        ClaimValue = defClaim.Last().Trim()
                    });
                }
            }

            var shouldCheck = (model.Claims != null) && model.Claims.Any();

            if (!CheckAdminWhen(shouldCheck, adminKey))
            {
                return(Unauthorized("Only admins can set claims."));
            }

            var createUserResult = await UserManager.CreateAsync(newUser, model.Password);

            if (createUserResult.Succeeded)
            {
                return(Ok(new LoginResponseModel
                {
                    UserName = newUser.UserName,
                    Email = newUser.Email,
                    Jwt = JwtProducer.Generate(newUser)
                }));
            }

            // Collects the errors
            foreach (var error in createUserResult.Errors)
            {
                ModelState.TryAddModelError(error.Code, error.Description);
            }

            return(BadRequest(ModelState));
        }