Exemplo n.º 1
0
        public IActionResult Signin(SigninModel model)
        {
            if (model.Mobile.Substring(5, 6) != model.Password)
            {
                return(Ok(new ApiResult <object>(ResultStatus.FAIL, "手机号或者密码不正确")));
            }

            ApiResult <WizardResp> wizardResult = _wizardService.GetWizard(model.Mobile, model.Password);

            if (wizardResult.Status != ResultStatus.SUCCESS)
            {
                return(Ok(new ApiResult <object>(ResultStatus.FAIL, "手机号或者密码不正确")));
            }

            ApiResult <ProfileResp> wizardInfoResult = _wizardService.GetProfile(wizardResult.Result.WizardId);

            if (wizardResult.Status != ResultStatus.SUCCESS)
            {
                return(Ok(new ApiResult <object>(ResultStatus.FAIL, "手机号或者密码不正确")));
            }

            ApiResult <IEnumerable <ApplicantResp> > applicatResult = _activityService.GetApplicants(model.Mobile);

            if (applicatResult.Status != ResultStatus.SUCCESS || applicatResult.Result.IsNullOrEmpty())
            {
                return(Ok(new ApiResult <object>(ResultStatus.FAIL, "未报名")));
            }

            var identity = new ClaimsIdentity(new GenericIdentity(model.Mobile, "Token"), new[]
            {
                new Claim("id", wizardResult.Result.WizardId.ToString()),
                new Claim("rol", "api_access"),
                new Claim(ClaimTypes.NameIdentifier, wizardResult.Result.WizardId.ToString()),
                new Claim(ClaimTypes.Name, wizardInfoResult.Result.NickName),
                new Claim(JwtRegisteredClaimNames.Sub, model.Mobile),
                new Claim(JwtRegisteredClaimNames.Jti, _jwtOptions.JtiGenerator()),
                new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64),
            });

            var handler = new JwtSecurityTokenHandler();

            SecurityToken securityToken = handler.CreateToken(new SecurityTokenDescriptor
            {
                Issuer             = _jwtOptions.Issuer,
                Audience           = _jwtOptions.Audience,
                SigningCredentials = _jwtOptions.SigningCredentials,
                NotBefore          = _jwtOptions.NotBefore,
                Subject            = identity,
                Expires            = _jwtOptions.Expiration,
            });

            string encodedJwt = handler.WriteToken(securityToken);

            return(Ok(new ApiResult <object>(ResultStatus.SUCCESS, new
            {
                id = identity.Claims.Single(c => c.Type == "id").Value,
                auth_token = encodedJwt,
                expires_in = (int)_jwtOptions.ValidFor.TotalSeconds
            })));
        }
Exemplo n.º 2
0
        private ClaimsIdentity GetClaimsIdentity(string account, string password)
        {
            if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password))
            {
                return(null);
            }

            ApiResult <WizardResp> result = _wizardService.GetWizard(account, password);

            if (result.Status != ResultStatus.SUCCESS || result.Result == null)
            {
                return(null);
            }

            return(_jwtFactory.GenerateClaimsIdentity(result.Result.Account, result.Result.WizardId));
        }
Exemplo n.º 3
0
        public IActionResult FindWizard(long wizardId)
        {
            ApiResult <WizardResp> result = _wizardService.GetWizard(wizardId);

            if (result.Status != ResultStatus.SUCCESS)
            {
                return(Fail(result.Message));
            }

            return(Ok(new
            {
                result.Result.Account,
                result.Result.WizardId,
                result.Result.Email,
                result.Result.CreateTime,
                result.Result.DivisionId
            }));
        }
Exemplo n.º 4
0
        public IActionResult WizardInfo(long wizardId)
        {
            if (wizardId <= 0)
            {
                return(Fail("请选择正确的巫师"));
            }

            ApiResult <WizardResp> wizard = _wizardService.GetWizard(wizardId);

            if (wizard.Status != ResultStatus.SUCCESS || wizard.Result == null)
            {
                return(Fail("巫师不存在"));
            }

            ApiResult <ProfileResp> profile = _wizardService.GetProfile(wizardId);

            if (profile.Status != ResultStatus.SUCCESS || wizard.Result == null)
            {
                return(Fail("查询不到个人资料"));
            }

            return(Ok(new
            {
                wizard.Result.WizardId,
                wizard.Result.Account,
                wizard.Result.DivisionId,
                wizard.Result.Email,
                Profile = new
                {
                    profile.Result.NickName,
                    profile.Result.Birthday,
                    profile.Result.Gender,
                    profile.Result.House,
                    profile.Result.Slogan,
                    profile.Result.PortraitUrl
                }
            }));
        }