コード例 #1
0
        public ActionResult Login([FromBody] Login login)
        {
            var identify = GetIdentity(login.Username, login.Password);

            if (identify == null)
            {
                return(BadRequest(new { errorText = "Неверный логин или пароль" }));
            }
            var now = DateTime.UtcNow;
            // создаем токен
            var jwt = new JwtSecurityToken(
                issuer: AuthOptions.ISSUER,
                audience: AuthOptions.AUDIENCE,
                notBefore: now,
                claims: identify.Claims,
                expires: now.Add(TimeSpan.FromMinutes(AuthOptions.LIFETIME_MINITE)),
                signingCredentials: new SigningCredentials(
                    AuthOptions.GetSymmetricSecurityKey(),
                    SecurityAlgorithms.HmacSha256));
            var  encodeJwt = new JwtSecurityTokenHandler().WriteToken(jwt);
            User user      = _context.Users.FirstOrDefault(u => u.UserName == login.Username);

            CheckUserInToken(user, ref this.userExisted);

            if (userExisted)
            {
                var currentToken = _context.Tokens.First(t => t.UserId == user.Id);
                currentToken.TokenValue = encodeJwt;
            }
            else
            {
                _context.Tokens.Add(new Token
                {
                    TokenValue = encodeJwt,
                    UserId     = user.Id,
                });
            }
            _context.SaveChanges();
            return(Ok(
                       new
            {
                access_token = encodeJwt,
                username = identify.Name,
                userRole = user.UserRole,
                id = user.Id,
            }));
        }
コード例 #2
0
        public int PostOrder(Order order)
        {
            DateTime now = DateTime.Now;

            _context.Orders.Add(new Order
            {
                ClientId    = order.ClientId,
                StatusId    = 1, // Принят в обаботку
                DateCreated = now
            });
            _context.SaveChanges();

            int currentOrderId = _context.Orders
                                 .Where(o => o.ClientId == order.ClientId && o.DateCreated == now)
                                 .Select(o => o.Id)
                                 .FirstOrDefault();

            return(currentOrderId);
        }