Esempio n. 1
0
        /// <summary>
        /// 生成 access_token(密码模式)
        /// </summary>
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            context.Response.Headers.Add("Access-Control-Allow-Origin", new string[] { "*" });
            AuthorityUserDal   _AuthorityUserDal   = new AuthorityUserDal();
            AuthorityOuUserDal _AuthorityOuUserDal = new AuthorityOuUserDal();
            AuthorityOuDal     _AuthorityOuDal     = new AuthorityOuDal();

            if (string.IsNullOrEmpty(context.UserName))
            {
                context.SetError("非法的用户名", "用户名不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(context.Password))
            {
                context.SetError("非法的密码", "密码不能为空!");
                return;
            }

            AuthorityUser user = _AuthorityUserDal.GetUser(context.UserName, context.Password);

            if (user != null)
            {
                List <int>         ouUser = _AuthorityOuUserDal.Get(o => o.userId == user.id).Select(o => o.ouId).ToList();
                List <AuthorityOu> ous    = _AuthorityOuDal.Get(o => ouUser.Contains(o.id)).ToList();

                var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
                identity.AddClaim(new Claim("AuthorityUser", JsonHelper.JsonSerialize(user)));
                identity.AddClaim(new Claim("AuthorityOu", JsonHelper.JsonSerialize(ous)));
                context.Validated(identity);
            }
            else
            {
                context.SetError("不合法的身份验证", "账号或密码不正确!");
            }
        }