Ejemplo n.º 1
0
        public IActionResult Create([FromBody] CreateUserRequest newUser)
        {
            string authorizationHeader = Request.Headers["Authorization"];

            if (authorizationHeader != null && authorizationHeader.StartsWith("Basic"))
            {
                string   encodedUsernamePassword = authorizationHeader.Substring("Basic ".Length).Trim();
                Encoding encoding         = Encoding.GetEncoding("iso-8859-1");
                string   usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword));

                int seperatorIndex = usernamePassword.IndexOf(':');

                string username = usernamePassword.Substring(0, seperatorIndex);
                string password = usernamePassword.Substring(seperatorIndex + 1);
            }
            else
            {
                throw new Exception("The authorization header is either empty or isn't Basic.");
            }


            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = _users.Create(newUser);

            var url = Url.Action("GetById", new { id = user.Id });
            var responseViewModel = new UserResponse(user);

            return(Created(url, responseViewModel));
        }
Ejemplo n.º 2
0
        public IActionResult CreateUser(CreateUserRequestModel newUser)
        {
            if (!ModelState.IsValid)
            {
                return(View("CreateUserPage", newUser));
            }

            _users.Create(newUser);
            return(RedirectToAction("UsersPage"));
        }
Ejemplo n.º 3
0
        public IActionResult Create([FromBody] CreateUserRequest newUser)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = _users.Create(newUser);

            var url = Url.Action("GetById", new { id = user.Id });
            var responseViewModel = new UserResponse(user);

            return(Created(url, responseViewModel));
        }
Ejemplo n.º 4
0
        public IActionResult CreateUser(CreateUserRequestModel newUser)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = _users.Create(newUser);

            var url = Url.Action("UserDetails", user.Id);
            var responseViewModel = new UserResponseModel(user);

            return(Created(url, responseViewModel));
        }
Ejemplo n.º 5
0
        public async Task <AuthenticationResult> Register(string email, string password, string name)
        {
            var existedUser = await _userManager.FindUserByEmail(email);

            if (existedUser != null)
            {
                return(new AuthenticationResult
                {
                    ErrorMessages = new[] { "Email already exists " }
                });
            }

            var newUser = new User()
            {
                Name  = name,
                Email = email
            };

            await _userManager.Create(newUser);


            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_jwtSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor()
            {
                Subject = new ClaimsIdentity(new[]
                {
                    new Claim(JwtRegisteredClaimNames.Sub, newUser.Email),
                    new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                    new Claim(JwtRegisteredClaimNames.Email, newUser.Email),
                    new Claim("id", newUser.Id)
                }),
                Expires            = DateTime.Now.AddHours(2),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key),
                                                            SecurityAlgorithms.HmacSha256Signature)
            };

            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(new AuthenticationResult
            {
                Success = true,
                Token = tokenHandler.WriteToken(token)
            });
        }
 public IActionResult CreateUser(CreateUserRequestModel newUser)
 {
     _users.Create(newUser);
     return(RedirectToAction("UsersPage"));
 }