Example #1
0
        public async Task <JObject> Login([FromBody] Post_UserViewModel obj)
        {
            DataResult result = new DataResult();

            result.verifiaction = false;
            try
            {
                string name     = obj.name;
                string password = obj.password;
                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
                {
                    result.message = "账号或者密码不能为空!";
                    return(JObject.FromObject(result));
                }
                var entity = _userRepsonsityService.Login(name, password);

                if (entity != null)
                {
                    //开始写入身份信息
                    var indenti = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);

                    //var userPrincipal = new ClaimsPrincipal(indenti);

                    indenti.AddClaim(new Claim(ClaimTypes.Name, entity.UserName));
                    //indenti.AddClaim(new Claim("password", entity.pa));
                    indenti.AddClaim(new Claim(ClaimTypes.NameIdentifier, entity.ID));
                    indenti.AddClaim(new Claim("email", entity.Email));
                    //HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(indenti));
                    await HttpContext.SignInAsync(indenti.AuthenticationType,
                                                  new ClaimsPrincipal(indenti),
                                                  new AuthenticationProperties
                    {
                        IsPersistent = true,
                        RedirectUri  = "/Home/Index",
                        ExpiresUtc   = new System.DateTimeOffset(dateTime: DateTime.Now.AddMinutes(30)),
                    });
                }

                result.verifiaction = true;
                result.message      = "登陆成功!";
            }
            catch (Exception ex)
            {
                result.message = "非法登陆!";
                return(JObject.FromObject(result));
            }
            finally
            {
            }
            return(JObject.FromObject(result));;
        }
Example #2
0
        public JObject Token1([FromBody] Post_UserViewModel obj)
        {
            DataResult result = new DataResult();

            result.verifiaction = false;
            try
            {
                string name     = obj.name;
                string password = obj.password;
                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
                {
                    result.message = "账号或者密码不能为空!";
                    return(JObject.FromObject(result));
                }

                var entity = _userRepsonsityService.Login(name, password);

                if (entity != null)
                {
                    Dictionary <string, object> payload = new Dictionary <string, object>();
                    payload.Add("ID", entity.ID);
                    payload.Add("UserName", entity.UserName);
                    payload.Add("Email", entity.Email);

                    var tokenacces = new
                    {
                        AccessToken = Encrypts.CreateToken(payload, 30),
                        Expires     = 3600
                    };
                    result.rows         = tokenacces;
                    result.verifiaction = true;
                    result.message      = "登陆成功!";
                }
                else
                {
                    result.message      = "获取token令牌失败!";
                    result.verifiaction = true;
                }
            }
            catch (Exception ex)
            {
                result.message = "非法登陆!";
                return(JObject.FromObject(result));
            }
            finally
            {
            }
            return(JObject.FromObject(result));
        }
Example #3
0
        public JObject GetToken([FromBody] Post_UserViewModel obj)
        {
            DataResult result = new DataResult
            {
                Verifiaction = false
            };

            try
            {
                string name     = obj.Name;
                string password = obj.Password;
                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
                {
                    result.Message = "账号或者密码不能为空!";
                    return(JObject.FromObject(result));
                }

                var entity = _userRepsonsityService.Login(name, password);

                if (entity != null)
                {
                    result.Rows         = _jwtAuthorization.CreateToken(entity);
                    result.Verifiaction = true;
                    result.Message      = "登陆成功!";
                }
                else
                {
                    result.Message      = "获取token令牌失败!";
                    result.Verifiaction = true;
                }
            }
            finally
            {
            }
            return(JObject.FromObject(result));
        }
Example #4
0
        public JObject Token([FromBody] Post_UserViewModel obj)
        {
            DataResult result = new DataResult();

            result.verifiaction = false;
            try
            {
                string name     = obj.name;
                string password = obj.password;
                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
                {
                    result.message = "账号或者密码不能为空!";
                    return(JObject.FromObject(result));
                }

                var entity = _userRepsonsityService.Login(name, password);

                if (entity != null)
                {
                    var claims = new Claim[]
                    {
                        new Claim(ClaimTypes.Name, entity.UserName),
                        new Claim(ClaimTypes.NameIdentifier, entity.ID.ToString()),
                    };

                    var key     = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(ConfigHelper.GetSectionValue("JwtSecurityKey")));
                    var expires = DateTime.UtcNow.AddDays(28);//
                    var token   = new JwtSecurityToken(
                        issuer: "issuer",
                        audience: "audience",
                        claims: claims,
                        notBefore: DateTime.Now,
                        expires: expires,
                        signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256));

                    //生成Token
                    string jwtToken   = new JwtSecurityTokenHandler().WriteToken(token);
                    var    tokenacces = new
                    {
                        AccessToken = jwtToken,
                        Expires     = DateTime.UtcNow.AddDays(28)
                    };
                    result.rows         = tokenacces;
                    result.verifiaction = true;
                    result.message      = "登陆成功!";
                }
                else
                {
                    result.message      = "获取token令牌失败!";
                    result.verifiaction = true;
                }
            }
            catch (Exception ex)
            {
                result.message = "非法登陆!";
                return(JObject.FromObject(result));
            }
            finally
            {
            }
            return(JObject.FromObject(result));
        }