Exemplo n.º 1
0
        public IActionResult Login(string username, string password)
        {
            try
            {
                using (dbContext)
                {
                    User user = dbContext.User.FirstOrDefault(x => x.LoginName == username.Trim());
                    if (user == null || !user.IsEnable)
                    {
                        return(FailResponse("UserNotExist"));
                    }

                    if (user.Password != password.Trim())
                    {
                        return(FailResponse("PasswordWrong"));
                    }

                    if (user.IsLocked)
                    {
                        return(FailResponse("Locked"));
                    }

                    if (!user.IsEnable)
                    {
                        return(FailResponse("UserDisable"));
                    }

                    var claimsIdentity = new ClaimsIdentity(new[]
                    {
                        new Claim(ClaimTypes.Name, username),
                        new Claim(nameof(Entities.User.Id), user.Id.ToString()),
                        new Claim(nameof(Entities.User.LoginName), user.LoginName),
                        new Claim(nameof(Entities.User.Password), user.Password),
                        new Claim(nameof(Entities.User.IsEnable), user.IsEnable.ToString()),
                    });

                    return(Ok(new
                    {
                        token = AuthenticationConfiguration.GetJwtAccessToken(appSettings, claimsIdentity),
                        code = (int)HttpStatusCode.OK,
                        message = "成功",
                    }));
                }
            }
            catch (System.Exception ex)
            {
                return(Ok(new
                {
                    message = ex.ToString(),
                }));
            }
        }