Ejemplo n.º 1
0
 public ResponseTokenModel Login(RequestLoginModel request)
 {
     try
     {
         string            result        = HttpUtils.PostRequest(AppConfigMoel.URL + ConstantsValue.HTTP_LOGIN_URI, JsonConvert.SerializeObject(request));
         BaseResponseModel responseModel = JsonConvert.DeserializeObject <BaseResponseModel>(result);
         if (responseModel != null)
         {
             if (responseModel.code.Equals("200"))
             {
                 ResponseTokenModel response = JsonConvert.DeserializeObject <ResponseTokenModel>(responseModel.data.ToString());
                 return(response);
             }
             else
             {
                 return(null);
             }
         }
         return(null);
     }
     catch (Exception)
     {
         throw;
     }
 }
Ejemplo n.º 2
0
        public ResponseTokenModel AuthenticateUser(string username, string password)
        {
            ResponseTokenModel response = new ResponseTokenModel();
            bool result = CheckUser(username: username, password: password);

            if (result == false)
            {
                response.Message = "Login Fail!";
                return(response);
            }
            var tokenHandler = new JwtSecurityTokenHandler();
            var key          = Encoding.ASCII.GetBytes("My Secret Key Personal Key");
            var claims       = new[] {
                new Claim("UserName", username),
                new Claim("Password", password),
                new Claim("CurrentDate", DateTime.Now.ToString()),
            };
            var token = new JwtSecurityToken("", "",
                                             claims,
                                             expires: DateTime.Now.AddDays(1),
                                             signingCredentials: new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                                             );

            response.Token    = tokenHandler.WriteToken(token);
            response.Message  = "Login Success";
            response.UserName = username;
            return(response);
        }
Ejemplo n.º 3
0
        public async Task <ResponseTokenModel> RefreshToken([FromBody] RefreshTokenDto input)
        {
            ResponseTokenModel model = new ResponseTokenModel
            {
                Success     = false,
                SetPassword = true
            };

            if (input == null)
            {
                model.Msg = L("UserCenter_ParamError");
                return(model);
            }
            if (string.IsNullOrWhiteSpace(input.ClientId))
            {
                model.Msg = L("UserCenter_ParamError");
                return(model);
            }

            var form = new Dictionary <string, string>();

            form["client_id"]     = input.ClientId;
            form["refresh_token"] = _cacheManager.GetCache(CacheKeyService.RefreshToken).Get(input.ClientId + input.UserId, () => string.Empty);

            //用户登陆
            if (input.ClientId == "app_customer_client")
            {
                form["client_secret"] = _appConfiguration["Customer:ClientSecrets"];
                form["grant_type"]    = "refresh_token";
            }
            //医生登陆
            if (input.ClientId == "app_doctor_client")
            {
                form["client_secret"] = _appConfiguration["Doctor:ClientSecrets"];
                form["grant_type"]    = "refresh_token";
            }

            var tokenModel = await _tokenService.RefreshToken(form, input.ClientId);

            if (tokenModel != null)
            {
                model.Success     = true;
                model.AccessToken = tokenModel.access_token;
                model.ExpiresIn   = tokenModel.expires_in;
                model.TokenType   = tokenModel.token_type;
                model.UserId      = input.UserId;

                List <string> proToken = _cacheManager.GetCache(CacheKeyService.DeviceToken).Get(input.ClientId + input.UserId + input.DeviceUUID, () => new List <string>());
                proToken.Add(tokenModel.access_token);
                _cacheManager.GetCache(CacheKeyService.DeviceToken).Set(input.ClientId + input.UserId + input.DeviceUUID, proToken);
                _cacheManager.GetCache(CacheKeyService.RefreshToken).Set(input.ClientId + input.UserId, tokenModel.refresh_token);
            }

            return(model);
        }
Ejemplo n.º 4
0
        public IActionResult AuthenticateUser(string username, string password)
        {
            ResponseTokenModel response = _httpClientService.AuthenticateUser(username: username, password: password);

            return(Ok(response));
        }
Ejemplo n.º 5
0
        public async Task <ResponseTokenModel> RequestToken([FromBody] RequestTokenDto input)
        {
            ResponseTokenModel model = new ResponseTokenModel
            {
                Success     = false,
                SetPassword = false
            };

            if (input == null)
            {
                model.Msg = L("UserCenter_ParamError");
                return(model);
            }
            if (string.IsNullOrWhiteSpace(input.ClientId))
            {
                model.Msg = L("UserCenter_ParamError");
                return(model);
            }

            var form = new Dictionary <string, string>();

            form["client_id"] = input.ClientId;

            //避免将refresh_token传递到客户端,使用用户Id+缓存的形式替代
            string userId = null;

            //用户登陆
            if (input.ClientId == "app_customer_client")
            {
                form["client_secret"] = _appConfiguration["Customer:ClientSecrets"];
                form["grant_type"]    = _appConfiguration["Customer:GrantType"];
                form["userInfo"]      = DesEncrypt.Encrypt("userInfo");
                form["userId"]        = "1";
            }
            //医生登陆
            if (input.ClientId == "app_doctor_client")
            {
                form["client_secret"] = _appConfiguration["Doctor:ClientSecrets"];
                form["grant_type"]    = _appConfiguration["Doctor:GrantType"];
                form["userInfo"]      = DesEncrypt.Encrypt("userInfo");
                form["userId"]        = "1";
            }

            var tokenModel = await _tokenService.RequestToken(form, input.ClientId);

            if (tokenModel != null)
            {
                model.Success     = true;
                model.AccessToken = tokenModel.access_token;
                model.ExpiresIn   = tokenModel.expires_in;
                model.TokenType   = tokenModel.token_type;
                model.UserId      = userId;

                List <string> deviceUsers = _cacheManager.GetCache(CacheKeyService.DeviceUser).Get(input.ClientId + userId, () => new List <string>());
                if (!deviceUsers.Contains(input.DeviceUUID))
                {
                    deviceUsers.Add(input.DeviceUUID);
                    _cacheManager.GetCache(CacheKeyService.DeviceUser).Set(input.ClientId + userId, deviceUsers);
                }

                //当前用户的token黑名单
                List <string> tokenBlacklist = _cacheManager.GetCache(CacheKeyService.BlacklistToken).Get(input.ClientId + userId, () => new List <string>());
                for (int i = 0; i < deviceUsers.Count; i++)
                {
                    var deviceUser = deviceUsers[i];
                    if (deviceUser.Equals(input.DeviceUUID))
                    {
                        List <string> curDeviceToken = _cacheManager.GetCache(CacheKeyService.DeviceToken).Get(input.ClientId + userId + deviceUser, () => new List <string>());
                        curDeviceToken.ForEach(p => tokenBlacklist.Remove(p));
                        continue;
                    }

                    //将其他所有设备的token放入黑名单
                    List <string> deviceToken = _cacheManager.GetCache(CacheKeyService.DeviceToken).Get(input.ClientId + userId + deviceUser, () => new List <string>());
                    if (deviceToken.Count > 0)
                    {
                        tokenBlacklist.AddRange(deviceToken);
                    }
                }
                _cacheManager.GetCache(CacheKeyService.BlacklistToken).Set(input.ClientId + userId, tokenBlacklist);

                //当前用户的token
                List <string> proToken = _cacheManager.GetCache(CacheKeyService.DeviceToken).Get(input.ClientId + userId + input.DeviceUUID, () => new List <string>());
                proToken.Add(tokenModel.access_token);
                _cacheManager.GetCache(CacheKeyService.DeviceToken).Set(input.ClientId + userId + input.DeviceUUID, proToken);

                _cacheManager.GetCache(CacheKeyService.RefreshToken).Set(input.ClientId + userId, tokenModel.refresh_token);
            }

            return(model);
        }