public async Task <TokenReponse> CreateToken(string username, string password)
        {
            User user = await UserManager.FindByNameAsync(username);

            var signInResult = await SignInManager.CheckPasswordSignInAsync(user, password, false);

            if (signInResult.Succeeded)
            {
                var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(TMSJwtTokens.Key));
                var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

                var claims = new[]
                {
                    new Claim(JwtRegisteredClaimNames.Sub, username),
                    new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                    new Claim(JwtRegisteredClaimNames.UniqueName, username)
                };
                var token = new JwtSecurityToken(
                    TMSJwtTokens.Issuer,
                    TMSJwtTokens.Audience,
                    claims,
                    expires: DateTime.UtcNow.AddMinutes(30),
                    signingCredentials: creds
                    );

                var results = new TokenReponse(new JwtSecurityTokenHandler().WriteToken(token), token.ValidTo);

                return(results);
            }

            return(null);
        }
Esempio n. 2
0
        public Base <object> Edit(UsersQuery model)
        {
            Meta meta     = new Meta();
            bool editNote = true;
            //取token值
            TokenReponse repository = new TokenReponse();
            Token        token      = repository.First(model.Token);

            if (token == null)
            {
                meta.ErrorCode = ErrorCode.LoginError.GetHashCode().ToString();
                meta.ErrorMsg  = EnumHelper.GetDescriptionFromEnumValue(ErrorCode.LoginError);
            }
            else
            {
                editNote = UserService.Edit(model, ref meta);
            }
            Base <object> result = new Base <object>
            {
                Meta = meta,
                Body = editNote
            };

            return(result);
        }
Esempio n. 3
0
        public Base <object> Inviter(UsersQuery model)
        {
            Meta meta = new Meta();
            PageModel <Users> list = new PageModel <Users>();

            //取token值
            TokenReponse repository = new TokenReponse();
            Token        token      = repository.First(model.Token);

            if (token == null)
            {
                meta.ErrorCode = ErrorCode.LoginError.GetHashCode().ToString();
                meta.ErrorMsg  = EnumHelper.GetDescriptionFromEnumValue(ErrorCode.LoginError);
            }
            else
            {
                model.UserID = token.UserId;
                list         = UserService.Inviter(model, ref meta);
            }
            Base <object> response = new Base <object>
            {
                Body = list,
                Meta = meta
            };

            return(response);
        }
Esempio n. 4
0
        //校验是否丢失 token
        public Token VerifyToken(string token, ref Meta meta)
        {
            TokenReponse repository = new TokenReponse();
            Token        _token     = repository.First(token);

            if (string.IsNullOrEmpty(_token.Key))
            {
                meta.ErrorCode = ErrorCode.Error1017.GetHashCode().ToString();
                meta.ErrorMsg  = EnumHelper.GetDescriptionFromEnumValue(ErrorCode.Error1017);
                return(null);
            }
            return(_token);
        }
Esempio n. 5
0
        public Base <LoginResponse> Login(Login login, string userType)
        {
            Meta meta = new Meta();
            Base <LoginResponse> response = new Base <LoginResponse>();

            //验证 验证码
            if (login.Captcha.ToLower() != "1")
            {
                //校验验证码
                if (!CheckCaptcha(CaptchaType.Login.ToString() + login.Phone, login.Captcha.ToString(), ref meta))
                {
                    return(response);
                }
            }
            var result = UserService.Login(userType, login.Phone, login.Password);

            response.Meta.ErrorCode = Convert.ToInt32(result.ErrorCode).ToString();
            if (!result.IsSuccess)
            {
                response.Meta.ErrorMsg = result.Message;
                return(response);
            }
            else
            {
                //将数据 放入 token中
                response.Body       = new LoginResponse();
                response.Body.Token = WebCommon.Md5String(result.ResultObject.UserID.ToString());
                response.Body.SetValue(result.ResultObject);

                TokenReponse tokenRepository = new TokenReponse();
                Token        token           = new Token();
                token.Key    = response.Body.Token;
                token.UserId = result.ResultObject.UserID;
                tokenRepository.Add(token);

                //将用户登录的数据存储到Memcache中.
                //MemcacheHelper.Set(token.Key, Common.SerializableHelper.SerializableToString(token), DateTime.Now.AddMinutes(20));
            }

            return(response);
        }