コード例 #1
0
        public async Task <IActionResult> Authorization([FromQuery] string login = "", [FromQuery] string password = "")
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (login == "" || password == "")
            {
                return(StatusCode(404));
            }


            var user = GetUser(login, password);

            if (user == null)
            {
                return(StatusCode(401));
            }

            var encodedJwt = AuthOptions.GetJWT(user);

            var response = new
            {
                accses_token = encodedJwt,
            };

            return(Ok(response));
        }
コード例 #2
0
        public async Task <IActionResult> RegistrationUser([FromBody] User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var equalUser = _context.Users.FirstOrDefaultAsync(u => u.Email == user.Email);

            if (equalUser.Result != null)
            {
                return(StatusCode(409));
            }

            user.Password = AuthOptions.ComputeHash(user.Password);
            var jwt = AuthOptions.GetJWT(user);

            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            var response = new
            {
                token = jwt
            };

            return(Ok(response));
        }
コード例 #3
0
        public async Task <ActionResult> Login([FromBody] CreateUserCommand command)
        {
            var user = await _mediator.Send(command);

            if (user == null)
            {
                throw new NotFoundException();
            }

            var jwt = AuthOptions.GetJWT(user);

            return(Json(new
            {
                access_token = jwt,
                username = user.FullName,
                role = user.Role
            }));
        }
コード例 #4
0
        public async Task <IActionResult> PostUser([FromBody] User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var equalUser = _context.Users.FirstOrDefaultAsync(u => u.Email == user.Email);

            if (equalUser.Result != null)
            {
                return(Conflict("Пользователь уже существует"));
            }

            user.Password = AuthOptions.ComputeHash(user.Password);
            var jwt = AuthOptions.GetJWT(user);

            var rootPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);

            rootPath = Directory.GetParent(rootPath).FullName;
            rootPath = Directory.GetParent(rootPath).FullName;
            rootPath = Directory.GetParent(rootPath).FullName;

            using (var fstream = new FileStream(rootPath + @"\etc\anon.png", FileMode.OpenOrCreate))
            {
                var byteArray = new byte[fstream.Length];
                fstream.Read(byteArray, 0, byteArray.Length);
                user.Avatar = byteArray;
            }

            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            var response = new
            {
                user.Id,
                email    = user.Email,
                name     = user.Name,
                lastName = user.LastName,
                token    = jwt
            };

            return(Ok(response));
        }
コード例 #5
0
        public async Task <IActionResult> Authorization([FromBody] AuthData data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = GetUser(data.Login, data.Password);

            if (user == null)
            {
                return(Forbid());
            }

            var encodedJwt = AuthOptions.GetJWT(user);

            var response = new
            {
                accses_token = encodedJwt,
            };

            return(Ok(response));
        }