public string GetToken(JsonDocument tokenModel)
 {
     if (!tokenModel.RootElement.TryGetProperty("Name", out JsonElement Name) || !tokenModel.RootElement.TryGetProperty("PassWord", out JsonElement PassWord))
     {
         return("token获取失败");
     }
     else
     {
         var name     = Name.GetString();
         var password = PassWord.GetString();
         var model    = projectInfos.FirstOrDefault(o => o.Name == name && o.PassWord == password);
         if (model != null)
         {
             Dictionary <string, string> clims = new Dictionary <string, string>
             {
                 { "ProjectName", name }
             };
             return(_jwt.GetToken(clims));
         }
         else
         {
             return("token获取失败");
         }
     }
 }
Esempio n. 2
0
        public async Task <ActionResult> Login([FromBody] AuthProps props)
        {
            string sessionId = props != null ? props.SessionId : null;

            if (sessionId == null)
            {
                return(BadRequest("Необходимо авторизоваться"));
            }

            Http­Client client = new Http­Client();

            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"));
            var response = client.GetAsync("http://my.bsu.ru/auth.php?id=" + sessionId).Result;

            if (response.IsSuccessStatusCode)
            {
                var stringAuth = await response.Content.ReadAsStringAsync();

                XDocument xdoc = XDocument.Parse(stringAuth);
                XElement  res  = xdoc.Descendants("result").FirstOrDefault();
                if (res != null)
                {
                    string state = res.Attributes().Where(q => q.Name == "state").Select(q => q.Value).FirstOrDefault();
                    if (state == "1")
                    {
                        var check = db.Placements
                                    .FromSql <Placement>("select * from dbo.pg_access_to_specialities({0})", props.EmploymentId)
                                    .Any();
                        if (check)
                        {
                            var identity = GetClaimsIdentity(props.EmploymentId);
                            return(Ok(jwt.GetToken(identity)));
                        }
                        else
                        {
                            return(BadRequest("Ваше подразделение не добавлено в список разрешенных, обратитесь в ЦИТиДО"));
                        }
                    }
                    else
                    {
                        return(StatusCode(403));
                    }
                }
            }
            return(Unauthorized());
        }
Esempio n. 3
0
        public IActionResult Login(LoginPara model)
        {
            //PC端医生使用手机号登录  Source_UserType_key
            var ckUser = $"loginFail:{UserSource.PC.ToInt32()}_{UserType.医生.ToInt32()}_{model.account_num}";
            var cnt    = CacheManager.Create().Get <int>(ckUser);

            if (cnt >= _jwt.TryCount)
            {
                //登录连续失败设置上限以上,锁定账号30分钟后才能再次登录
                return(Json(new ResponseModel(ResponseCode.NotAuthority, $"当前账户连续登录失败{_jwt.TryCount}次以上,请30分钟后再尝试.")));
            }
            //调用登录
            var loginInfo = this.GetInstance <ILogin>();
            var result    = new ResponseModel(ResponseCode.Success, "人员登录成功!");

            try
            {
                var logicResult = loginInfo.DoctorLogin(model.account_num, model.password)?.Result;
                //登录成功
                if (logicResult.code == ResponseCode.Success)
                {
                    //登录成功 ,登录失败次数清零
                    CacheManager.Create().Remove(ckUser);
                    var loginKey = model.account_num;
                    //生成jwt
                    Dictionary <string, string> clims = new Dictionary <string, string>();
                    //Source_UserType_key
                    clims.Add("userName", $"userinfo:{UserSource.PC.ToInt32()}_{UserType.医生.ToInt32()}_{model.account_num}");
                    //获取到一个新的token
                    var value = _jwt.GetToken(clims);
                    //token添加到缓存中(设置缓存2小时失效)
                    AddLoginCacheToken(loginKey, value, UserType.医生, UserSource.PC);
                    //初始化一次用户登录信息
                    var accountObj = this.GetInstance <ISysUser>();
                    var userModel  = accountObj.GetAccountInfo(model.account_num)?.Result;
                    //输出信息返回token
                    logicResult.data.is_super_admin = userModel.is_super_admin;
                    //将token赋值给登录返回信息
                    logicResult.data.token = value;
                    result.data            = logicResult.data;
                    //返回结果
                    return(Json(result));
                }
                else
                {
                    cnt++;
                    //缓存有效期设置为30分钟
                    CacheManager.Create().Set(ckUser, cnt, new TimeSpan(0, 30, 0));
                    return(Json(new ResponseModel(ResponseCode.NotAuthority, $"登录失败,还有{_jwt.TryCount - cnt}次机会!失败原因:{logicResult.msg}")));
                }
            }
            catch (Exception ex)
            {
                result.code = ResponseCode.Error.ToInt32();
                result.msg  = "人员登录失败:" + ex.Message;
            }
            return(Json(result));
        }
Esempio n. 4
0
        public IActionResult UserAuth(AuthPara model)
        {
            //机构人员使用账户登录  Source_key
            var ckUser = $"authFail:{this.userAppLyInfo.sourse}_{model.account}";
            var cnt    = CacheManager.Create().Get <int>(ckUser);

            if (cnt >= _jwt.TryCount)
            {
                //登录连续失败设置上限以上,锁定账号30分钟后才能再次登录
                return(Json(new ResponseModel(ResponseCode.NotAuthority, $"当前账户连续获取身份令牌失败{_jwt.TryCount}次以上,请30分钟后再尝试.")));
            }
            //调用登录
            var authLogic = this.GetInstance <IAuthLogic>();
            var result    = new ResponseModel(ResponseCode.Success, "平台统一身份认证成功!");

            try
            {
                var authResult = authLogic.GetUserAuth(model.account, model.password)?.Result;
                //登录成功
                if (authResult.code == ResponseCode.Success)
                {
                    //登录成功 ,登录失败次数清零
                    CacheManager.Create().Remove(ckUser);
                    var loginKey = model.account;
                    //生成jwt
                    Dictionary <string, string> clims = new Dictionary <string, string>();
                    //Source_key
                    clims.Add("sourse", this.userAppLyInfo.sourse.ToString());
                    clims.Add("account", model.account);
                    //获取到一个新的token
                    var value = _jwt.GetToken(clims);
                    //服务器token有效期赋值
                    authResult.data.token_effective_period = Tools.ConvertDateTimeToInt(DateTime.Now.AddHours(2));
                    //将token赋值给登录返回信息
                    authResult.data.token = value;
                    result.data           = authResult.data;
                    //token添加到缓存中(设置缓存2小时失效)
                    AddAuthCacheToken(loginKey, result.data.ToJson());
                    //返回结果
                    return(Json(result));
                }
                else
                {
                    cnt++;
                    //缓存有效期设置为30分钟
                    CacheManager.Create().Set(ckUser, cnt, new TimeSpan(0, 30, 0));
                    return(Json(new ResponseModel(ResponseCode.NotAuthority, $"平台统一身份认证失败,还有{_jwt.TryCount - cnt}次机会!失败原因:{authResult.msg}")));
                }
            }
            catch (Exception ex)
            {
                result.msg  = "平台统一身份认证失败:" + ex.Message;
                result.code = ResponseCode.Error.ToInt32();
                Logger.Instance.Error(result.msg, ex);
            }
            return(Json(result));
        }
Esempio n. 5
0
        public async Task <ActionResult <string> > GetLogin(string userName, string passWord)
        {
            var user = await _service.Login(userName, passWord);

            if (user == null)
            {
                return(NotFound());
            }
            return(Ok(_jwt.GetToken(user)));
        }
        public IActionResult GetToken(User postuser)
        {
            var response = ResponseModelFactory.CreateInstance;
            var userInfo = new CoreUser
            {
                LoginName = postuser.userName,
                Password  = postuser.password
            };
            var user = _userAppService.GetUser(userInfo.LoginName);

            if (user != null)
            {
                Dictionary <string, string> clims = new Dictionary <string, string>();
                clims.Add("userId", user.Id.ToString());
                clims.Add("username", user.DisplayName);
                //写入缓存
                _redis.Set(user.Id, user);
                response.SetData(_jwt.GetToken(clims));
                return(Ok(response));
            }
            response.SetError();
            return(Ok(response));
        }