/// <summary> /// 获取用户的登录信息 /// </summary> /// <param name="bThrowException"></param> /// <returns></returns> public UserSelf GetUserSelf(bool bThrowException = true) { Models.UserSelf rlt = new UserSelf { OpId = 0, OpMan = "UNKNOW" }; try { if (_httpContext.User != null && _httpContext.User.Identity.IsAuthenticated) { return new Models.UserSelf { LoginId = uvInt("LoginId"), OpId = uvInt("OpId"), DoctorId = uvInt("DoctorId"), OpMan = _httpContext.User.Identity.Name, StationId = uvInt("StationId"), DrugStoreStationId = uvInt("DrugStoreStationId"), StationName = uvStr("StationName"), StationTypeId = uvInt("StationTypeId"), SelectedDepartmentId = uvIntN("SelectedDepartmentId"), SelectedDepartmentName = uvStr("SelectedDepartmentName"), LoginTime = uvDateTime("LoginTime"), Gender = uvInt("Gender"), Birthday = uvDateTime("Birthday"), IsCanTreat = uvBool("IsCanTreat"), IsManageUnit = uvBool("IsManageUnit"), MyRoleIds = uvIntC("MyRoleIds"), MyRoleNames = uvStrC("MyRoleNames"), PostTitleName = uvStr("PostTitleName"), PhotoUrlDef = uvStr("PhotoUrlDef"), MySonStations = uvIntC("MySonStations"), MyAllowStationIds = uvIntC("MyAllowStationIds"), DoctorAppId = uvStr("DoctorAppId"), //辅助登录 LoginExtId = uvInt("LoginExtId"), LoginExtMobile = uvStr("LoginExtMobile"), LoginExtName = uvStr("LoginExtName"), LoginExtFuncKeys = uvStr("LoginExtFuncKeys") } } ; else { throw new Exception("没有通过权限验证"); } } catch (Exception ex) { if (bThrowException) { throw new ComException(ExceptionTypes.Error_Unauthorized, "没有获取到权限数据", ex); } } return(rlt); }
/// <summary> /// 用户登录Token /// </summary> /// <param name="loginInfo"></param> /// <param name="expires"></param> /// <returns></returns> public Token GenerateUserLoginToken(UserSelf dd, TimeSpan?expires = null) { var handler = new JwtSecurityTokenHandler(); //添加各种验证 var claims = new List <Claim>(); Action <string, object> claimsAdd = (key, val) => { string v = ""; if (val is DateTime || val is DateTime?) { v = ((DateTime)val).Ticks.ToString(); } else if (val is IEnumerable <int> ) { v = string.Join(",", (IEnumerable <int>)val); } else if (val is IEnumerable <string> ) { v = string.Join(",", (IEnumerable <string>)val); } else { v = Ass.P.PStr(val); } claims.Add(new Claim(key, v)); }; //公共部分 claimsAdd(ClaimTypes.Role, _settings.Role); claims.Add(new Claim(ClaimTypes.NameIdentifier, dd.CustomerId.ToString(), ClaimValueTypes.Integer, Global.AUTHENTICATION_ISSUER)); claims.Add(new Claim(ClaimTypes.Name, dd.CustomerName ?? "", ClaimValueTypes.String, Global.AUTHENTICATION_ISSUER)); // claims.Add(new Claim(ClaimTypes.Role, userLoginData.RoleName ?? "", ClaimValueTypes.String, Global.AUTHENTICATION_ISSUER)); claimsAdd("LoginId", dd.LoginId); claimsAdd("OpId", dd.CustomerId); claimsAdd("DoctorId", dd.DoctorId); claimsAdd("OpMan", dd.CustomerName); claimsAdd("Gender", dd.Gender); claimsAdd("Birthday", dd.Birthday); claimsAdd("PostTitleName", dd.PostTitleName); claimsAdd("PhotoUrlDef", dd.PhotoUrlDef); claimsAdd("DoctorAppId", dd.DoctorAppId);//app端的用户Id claimsAdd("StationId", dd.StationId); claimsAdd("DrugStoreStationId", dd.DrugStoreStationId); //药品药房Id claimsAdd("StationName", dd.StationName); //工作站名称 claimsAdd("StationTypeId", dd.StationTypeId); claimsAdd("LoginTime", DateTime.Now); claimsAdd("IsCanTreat", dd.IsCanTreat); claimsAdd("IsManageUnit", dd.IsManageUnit); claimsAdd("MyAllowStationIds", dd.MyAllowStationIds); claimsAdd("MySonStations", dd.MySonStations); claimsAdd("SelectedDepartmentId", dd.SelectedDepartmentId);//选择的部门 claimsAdd("SelectedDepartmentName", dd.SelectedDepartmentName); claimsAdd("MyRoleIds", dd.MyRoleIds); claimsAdd("MyRoleNames", dd.MyRoleNames); //辅助登录 claimsAdd("LoginExtId", dd.LoginExtId); claimsAdd("LoginExtMobile", dd.LoginExtMobile); claimsAdd("LoginExtName", dd.LoginExtName); claimsAdd("LoginExtFuncKeys", dd.LoginExtFuncKeys); var userIdentity = new ClaimsIdentity(Global.JWT_LOGIN_USER_CLAIMS_IDENTITY);//其他都可以,主要獲取時候方便 userIdentity.AddClaims(claims); //ClaimsIdentity identity = new ClaimsIdentity( // new GenericIdentity(loginInfo.CustomerId), // new[] { new Claim(ClaimTypes.Sid, id), new Claim(ClaimTypes.Role, _settings.Role) } //); var skey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_settings.SecurityKey)); var SigningCredentials = new SigningCredentials(skey, SecurityAlgorithms.HmacSha256); var securityToken = handler.CreateToken(new SecurityTokenDescriptor { Issuer = _settings.Issuer, Audience = _settings.Audience, SigningCredentials = SigningCredentials, Subject = userIdentity, Expires = expires.HasValue ? DateTime.Now.Add(expires.Value) : DateTime.Now.AddHours(_settings.ExpiresHours) }); return(new Token { AccessToken = handler.WriteToken(securityToken), ExpiresTime = securityToken.ValidTo }); }
public UserFrameManager(UserSelf ud) { this.UserSelf = ud; }