public IActionResult WXOpenAuth(string openid) { var response = ResponseModelFactory.CreateInstance; SystemUser user; using (_dbContext) { user = _dbContext.SystemUser.FirstOrDefault(x => x.Wechat == openid); if (user == null) { response.SetFailed("需要微信授权登录!"); return(Ok(response)); } else { //获取权限名 string roleid = user.SystemRoleUuid.Trim(); string rolename = ""; if (!string.IsNullOrEmpty(roleid)) { rolename = _dbContext.SystemRole.FirstOrDefault(x => x.SystemRoleUuid == Guid.Parse(roleid)).RoleName; } var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.LoginName), new Claim("guid", user.SystemUserUuid.ToString()), new Claim("avatar", ""), new Claim("displayName", user.RealName), new Claim("loginName", user.LoginName), new Claim("emailAddress", ""), //new Claim("guid",user.SystemUserUuid.ToString()), new Claim("userType", ((int)user.UserType).ToString()), new Claim("roleid", (user.SystemRoleUuid.TrimEnd(','))), new Claim("roleName", (rolename.TrimEnd(','))), //new Claim("schoolguid",user.SchoolUuid!=null?user.SchoolUuid.ToString():""), }); var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity); response.SetData(new { access = new string[] { }, user_guid = user.SystemUserUuid, user_name = user.LoginName, user_type = user.UserType, permissions = "null", roleName = GetroleName(user.SystemRoleUuid), address = user.Address, tokens = token, phone = user.Phone, schoolguid = user.SchoolUuid, openid, idCard = user.UserIdCard, }); } return(Ok(response)); } }
/// <summary> /// 对指定实体的用户签署Bearer Token /// </summary> /// <param name="user"></param> /// <returns></returns> private string TokenBulider(DncUser user) { if (user == null || user.IsDeleted == IsDeleted.Yes) { throw new InvalidOperationException("用户不存在"); } if (user.IsLocked == IsLocked.Locked) { throw new InvalidOperationException("账号已被锁定"); } if (user.Status == UserStatus.Forbidden) { throw new InvalidOperationException("账号已被禁用"); } ClaimsIdentity claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.LoginName), new Claim("guid", user.Guid.ToString()), new Claim("avatar", ""), new Claim("displayName", user.DisplayName), new Claim("loginName", user.LoginName), new Claim("emailAddress", ""), new Claim("guid", user.Guid.ToString()), new Claim("userType", ((int)user.UserType).ToString()), }); return(JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity)); }
public IActionResult Auth([FromBody] OauthViewModel viewModel) { var response = ResponseModelFactory.CreateInstance; var user = _sysUserRepo.FindEntity(x => x.LoginName == viewModel.UserName && x.Password == viewModel.Password ); if (user == null) { response.SetNotFound("用户名密码不正确"); return(Ok(response)); } var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, viewModel.UserName), new Claim("id", user.Id.ToString()), new Claim("avatar", ""), new Claim("LoginName", user.LoginName), new Claim("displayName", user.DisplayName), new Claim("userType", ((int)user.UserType).ToString()) }); var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity); Log(user.Id, user.LoginName + "_" + user.DisplayName); var result = new { token, user.DisplayName }; response.SetData(result); return(Ok(response)); }
public async Task <IActionResult> Auth(AuthModel model) { var username = model.userName; var password = model.password; var response = ResponseModelFactory.CreateInstance; DncUser user; //RSAHelper rSAHelper = new RSAHelper // (RSAType.RSA, Encoding.UTF8, CeyhConfiguration.TheRSASetting.Private, CeyhConfiguration.TheRSASetting.Public); await using (_dbContext) { user = await _dbContext.DncUser.FirstOrDefaultAsync(x => x.LoginName == username.Trim()); if (user == null || user.IsDeleted == IsDeleted.Yes) { response.SetFailed("用户不存在"); return(Ok(response)); } var userP = _rSaHelper.Decrypt(user.Password); var modelP = _rSaHelper.Decrypt(password.Trim()); //var s1 = rSAHelper.Decrypt(password.Trim()); if (userP != modelP) { response.SetFailed("密码不正确"); return(Ok(response)); } if (user.IsLocked == IsLocked.Locked) { response.SetFailed("账号已被锁定"); return(Ok(response)); } if (user.Status == UserStatus.Forbidden) { response.SetFailed("账号已被禁用"); return(Ok(response)); } } var claimsIdentity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, username), new Claim("guid", user.Guid.ToString()), new Claim("avatar", ""), new Claim("displayName", user.DisplayName), new Claim("loginName", user.LoginName), new Claim("emailAddress", ""), new Claim("guid", user.Guid.ToString()), new Claim("userType", ((int)user.UserType).ToString()) }); var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity); response.SetData(token); return(Ok(response)); }
public IActionResult Auth(string username, string password) { var response = ResponseModelFactory.CreateInstance; DncUser user; try { using (_dbContext) { user = _dbContext.DncUser.FirstOrDefault(x => x.LoginName == username.Trim()); if (user == null || user.IsDeleted == IsDeleted.Yes) { response.SetFailed("用户不存在"); return(Ok(response)); } if (user.Password != password.Trim()) { response.SetFailed("密码不正确"); return(Ok(response)); } if (user.IsLocked == IsLocked.Locked) { response.SetFailed("账号已被锁定"); return(Ok(response)); } if (user.Status == UserStatus.Forbidden) { response.SetFailed("账号已被禁用"); return(Ok(response)); } } var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, username), new Claim("guid", user.Guid.ToString()), new Claim("avatar", ""), new Claim("displayName", user.DisplayName), new Claim("loginName", user.LoginName), new Claim("emailAddress", ""), new Claim("guid", user.Guid.ToString()), new Claim("userType", ((int)user.UserType).ToString()) }); var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity); response.SetData(token); return(Ok(response)); } catch (System.Exception ee) { response.SetError(ee.Message); return(Ok(response)); } }
public async Task <IActionResult> Login([FromBody] LoginInfo loginInfo) { var user = await _context.Users.FirstOrDefaultAsync(u => u.Account == loginInfo.Account.Trim()); if (user == null) { return(BadRequest("账号不存在")); } if (user.Password != loginInfo.Password) { return(BadRequest("账号或密码错误")); } if (user.Status != 0) { return(BadRequest("账号状态异常")); } var data = new LoginResultViewModel { Avatar = user.Avatar ?? string.Empty, Id = user.Id, NiName = user.NiName ?? string.Empty, }; var exp = $"{new DateTimeOffset(DateTime.Now.AddMinutes(_settings.ExpMinutes)).ToUnixTimeSeconds()}"; var claims = new List <Claim> { new Claim(JwtRegisteredClaimNames.Jti, user.Id.ToString()), new Claim("niName", user.NiName ?? string.Empty), new Claim("avatar", user.Avatar ?? string.Empty), new Claim(JwtRegisteredClaimNames.Iss, _settings.Issuer), new Claim(JwtRegisteredClaimNames.Aud, _settings.Audience), new Claim(JwtRegisteredClaimNames.Exp, exp), }; // 生成token var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_settings, claims); data.Token = token; return(Ok(data)); }
public IActionResult Login(LoginModel model) { var response = new ResponseResult <LoginResult>(); //if (!ModelState.IsValid) //{ // return Ok(response.Error(ResponseCode.INVALID_PARAMS, GetErrorMsgFromModelState())); //} SysUser user; using (_dbContext) { user = _dbContext.SysUser.FirstOrDefault(x => x.UserName == model.UserName); if (user == null || (user.DelFlag.HasValue && user.DelFlag.Value == 1)) { return(Ok(response.Error(ResponseCode.USER_NOT_EXIST, "用户不存在"))); } if (user.Password?.ToLower() != (model.Password + user.Salt).ToMd5()) { return(Ok(response.Error(ResponseCode.USER_NOT_EXIST, "密码错误"))); } } var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.PrimarySid, user.Id.ToString()), new Claim(ClaimTypes.NameIdentifier, user.UserName ?? string.Empty), new Claim(ClaimTypes.Email, user.Email ?? string.Empty), new Claim(ClaimTypes.Name, user.Nickname ?? string.Empty), new Claim(ClaimTypes.Role, user.UserType.ToString()), new Claim("Avatar", user.Avatar ?? string.Empty), }); var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity); return(Ok(response.Success("登录成功", new LoginResult() { Nickname = user.Nickname, Token = token, UserId = user.Id, UserName = user.UserName }))); }
public IActionResult Auth(UserData userdata) { var response = ResponseModelFactory.CreateInstance; SystemUser user; using (_dbContext) { user = _dbContext.SystemUser.FirstOrDefault(x => x.LoginName == userdata.username); if (user == null || user.IsDeleted == 1) { response.SetFailed("用户不存在"); return(Ok(response)); } string s = DesEncrypt.Encrypt(userdata.password.Trim(), MdDesEncrypt.SecretKey); if (user.PassWord != DesEncrypt.Encrypt(userdata.password.Trim(), MdDesEncrypt.SecretKey)) { response.SetFailed("密码不正确"); return(Ok(response)); } if (userdata.type == "5") { response.SetFailed("非系统用户"); return(Ok(response)); } School school = new School(); school.SchoolName = ""; if (user.SchoolUuid != null) { school = _dbContext.School.FirstOrDefault(x => x.SchoolUuid == user.SchoolUuid); } //if (user.IsLocked == CommonEnum.IsLocked.Locked) //{ // response.SetFailed("账号已被锁定"); // return Ok(response); //} //if (user.Status == UserStatus.Forbidden) //{ // response.SetFailed("账号已被禁用"); // return Ok(response); //} //获取权限名 string[] roleid = user.SystemRoleUuid.TrimEnd(',').Split(","); string rolename = ""; for (int i = 0; i < roleid.Length; i++) { if (!string.IsNullOrEmpty(roleid[i])) { rolename += _dbContext.SystemRole.FirstOrDefault(x => x.SystemRoleUuid == Guid.Parse(roleid[i])).RoleName + ","; } } string superAdmin = ""; //超管roleid var temp5 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("超级")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp5.Count > 0) { superAdmin = temp5[0].SystemRoleUuid.ToString(); } int usertype = 0; if (!user.SystemRoleUuid.Contains(superAdmin)) { usertype = 2; } var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, userdata.username), new Claim("guid", user.SystemUserUuid.ToString()), new Claim("avatar", ""), new Claim("displayName", user.RealName), new Claim("loginName", user.LoginName), new Claim("emailAddress", ""), new Claim("userType", user.UserType.Value.ToString()), new Claim("roleid", user.SystemRoleUuid.TrimEnd(',')), new Claim("roleName", rolename.TrimEnd(',')), new Claim("schoolguid", user.SchoolUuid != null?user.SchoolUuid.ToString():""), new Claim("schoolName", school.SchoolName), }); var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity); response.SetData(token); return(Ok(response)); } }
public IActionResult WXAuth2(UserData userdata) { var response = ResponseModelFactory.CreateInstance; SystemUser user; using (_dbContext) { user = _dbContext.SystemUser.FirstOrDefault(x => x.Phone == userdata.username.Trim()); if (user == null || user.IsDeleted == 1) { response.SetFailed("用户不存在"); return(Ok(response)); } if (user.PassWord != Haikan3.Utils.DesEncrypt.Encrypt(userdata.password.Trim(), MdDesEncrypt.SecretKey)) { response.SetFailed("密码不正确"); return(Ok(response)); } //获取权限名 string[] roleid = user.SystemRoleUuid.TrimEnd(',').Split(","); string rolename = ""; for (int i = 0; i < roleid.Length; i++) { if (!string.IsNullOrEmpty(roleid[i])) { rolename += _dbContext.SystemRole.FirstOrDefault(x => x.SystemRoleUuid == Guid.Parse(roleid[i])).RoleName + ","; } } string zyz = ""; string yh = ""; string ddy = ""; string sj = ""; var temp1 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("志愿者")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp1.Count > 0) { zyz = temp1[0].SystemRoleUuid.ToString(); } var temp2 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("用户")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp2.Count > 0) { yh = temp2[0].SystemRoleUuid.ToString(); } var temp3 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("督导员")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp3.Count > 0) { ddy = temp3[0].SystemRoleUuid.ToString(); } var temp4 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("商")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp4.Count > 0) { sj = temp4[0].SystemRoleUuid.ToString(); } var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, userdata.username), new Claim("guid", user.SystemUserUuid.ToString()), new Claim("avatar", ""), new Claim("displayName", user.RealName), new Claim("loginName", user.LoginName), new Claim("emailAddress", ""), //new Claim("guid",user.SystemUserUuid.ToString()), new Claim("userType", ((int)user.UserType).ToString()), new Claim("roleid", (user.SystemRoleUuid.TrimEnd(','))), new Claim("roleName", (rolename.TrimEnd(','))), }); var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity); response.SetData(token); return(Ok(response)); } }
public IActionResult UnifyAuth(string unifyToken) { var response = ResponseModelFactory.CreateInstance; PassportServiceSoapClient client = new PassportServiceSoapClient(EndpointConfiguration.PassportServiceSoap, "https://sso.haikan.com.cn/api/passportservice.asmx"); var data = client.TokenGetCertAsync(unifyToken).Result; if (data.Body.TokenGetCertResult == null) { response.SetFailed("token无效请重新登录"); return(Ok(response)); } BlowFish blowFish = new BlowFish(); var info = blowFish.Decrypt("haikan051030", data.Body.TokenGetCertResult.ToString()); var arr = info.Split('&'); UserData userdata = new UserData() { username = arr[0], password = arr[1], }; SystemUser user; using (_dbContext) { user = _dbContext.SystemUser.FirstOrDefault(x => x.LoginName == userdata.username.Trim()); if (user == null || user.IsDeleted == 1) { response.SetFailed("用户不存在"); return(Ok(response)); } //string s = Haikan3.Utils.DesEncrypt.Encrypt(userdata.password.Trim(), MdDesEncrypt.SecretKey); string s = Security.GenerateMD5(userdata.password.Trim()); //if (user.PassWord != Haikan3.Utils.DesEncrypt.Encrypt(userdata.password.Trim(), MdDesEncrypt.SecretKey)) if (user.PassWord != Security.GenerateMD5(userdata.password.Trim())) { response.SetFailed("密码不正确"); return(Ok(response)); } //if (user.IsLocked == CommonEnum.IsLocked.Locked) //{ // response.SetFailed("账号已被锁定"); // return Ok(response); //} //if (user.Status == UserStatus.Forbidden) //{ // response.SetFailed("账号已被禁用"); // return Ok(response); //} //获取权限名 string[] roleid = user.SystemRoleUuid.TrimEnd(',').Split(","); string rolename = ""; for (int i = 0; i < roleid.Length; i++) { if (!string.IsNullOrEmpty(roleid[i])) { rolename += _dbContext.SystemRole.FirstOrDefault(x => x.SystemRoleUuid == Guid.Parse(roleid[i])).RoleName + ","; } } string zyz = ""; string yh = ""; string ddy = ""; string sj = ""; string superAdmin = ""; //超管roleid var temp5 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("超级")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp5.Count > 0) { superAdmin = temp5[0].SystemRoleUuid.ToString(); } int usertype = 0; if (!user.SystemRoleUuid.Contains(superAdmin)) { usertype = 2; } var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, userdata.username), new Claim("guid", user.SystemUserUuid.ToString()), new Claim("avatar", ""), new Claim("displayName", user.RealName), new Claim("loginName", user.LoginName), new Claim("emailAddress", ""), //new Claim("guid",user.SystemUserUuid.ToString()), //new Claim("userType",usertype.ToString()), new Claim("userType", user.UserType.Value.ToString()), new Claim("roleid", user.SystemRoleUuid.TrimEnd(',')), new Claim("roleName", rolename.TrimEnd(',')), new Claim("ZYZ", zyz), new Claim("YH", yh), new Claim("DDY", ddy), new Claim("SJ", sj) }); var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity); response.SetData(token); return(Ok(response)); } }
public IActionResult Auth(UserData userdata) { var response = ResponseModelFactory.CreateInstance; SystemUser user; using (_dbContext) { user = _dbContext.SystemUser.FirstOrDefault(x => x.LoginName == userdata.username.Trim()); if (user == null || user.IsDeleted == 1) { response.SetFailed("用户不存在"); return(Ok(response)); } //string s = Haikan3.Utils.DesEncrypt.Encrypt(userdata.password.Trim(), MdDesEncrypt.SecretKey); string s = Security.GenerateMD5(userdata.password.Trim()); //if (user.PassWord != Haikan3.Utils.DesEncrypt.Encrypt(userdata.password.Trim(), MdDesEncrypt.SecretKey)) if (user.PassWord != Security.GenerateMD5(userdata.password.Trim())) { response.SetFailed("密码不正确"); return(Ok(response)); } //if (user.IsLocked == CommonEnum.IsLocked.Locked) //{ // response.SetFailed("账号已被锁定"); // return Ok(response); //} //if (user.Status == UserStatus.Forbidden) //{ // response.SetFailed("账号已被禁用"); // return Ok(response); //} //获取权限名 string[] roleid = user.SystemRoleUuid.TrimEnd(',').Split(","); string rolename = ""; for (int i = 0; i < roleid.Length; i++) { if (!string.IsNullOrEmpty(roleid[i])) { rolename += _dbContext.SystemRole.FirstOrDefault(x => x.SystemRoleUuid == Guid.Parse(roleid[i])).RoleName + ","; } } string zyz = ""; string yh = ""; string ddy = ""; string sj = ""; //志愿者roleid var temp1 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("志愿者")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp1.Count > 0) { zyz = temp1[0].SystemRoleUuid.ToString(); } //普通用户roleid var temp2 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("用户")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp2.Count > 0) { yh = temp2[0].SystemRoleUuid.ToString(); } //督导员roleid var temp3 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("督导员")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp3.Count > 0) { ddy = temp3[0].SystemRoleUuid.ToString(); } //商户 var temp4 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("商")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp4.Count > 0) { sj = temp4[0].SystemRoleUuid.ToString(); } string superAdmin = ""; //超管roleid var temp5 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("超级")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp5.Count > 0) { superAdmin = temp5[0].SystemRoleUuid.ToString(); } int usertype = 0; if (!user.SystemRoleUuid.Contains(superAdmin)) { usertype = 2; } var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, userdata.username), new Claim("guid", user.SystemUserUuid.ToString()), new Claim("avatar", ""), new Claim("displayName", user.RealName), new Claim("loginName", user.LoginName), new Claim("emailAddress", ""), //new Claim("guid",user.SystemUserUuid.ToString()), //new Claim("userType",usertype.ToString()), new Claim("userType", user.UserType.Value.ToString()), new Claim("roleid", user.SystemRoleUuid.TrimEnd(',')), new Claim("roleName", rolename.TrimEnd(',')), new Claim("ZYZ", zyz), new Claim("YH", yh), new Claim("DDY", ddy), new Claim("SJ", sj) }); var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity); response.SetData(token); return(Ok(response)); } }
public IActionResult WXOpenAuth(string openid) { var response = ResponseModelFactory.CreateInstance; SystemUser user; using (_dbContext) { user = _dbContext.SystemUser.FirstOrDefault(x => x.Wechat == openid); if (user == null) { response.SetFailed("需要微信授权登录!"); return(Ok(response)); } else { //获取权限名 string[] roleid = user.SystemRoleUuid.TrimEnd(',').Split(","); string rolename = ""; for (int i = 0; i < roleid.Length; i++) { if (!string.IsNullOrEmpty(roleid[i])) { rolename += _dbContext.SystemRole.FirstOrDefault(x => x.SystemRoleUuid == Guid.Parse(roleid[i])).RoleName + ","; } } string zyz = ""; string yh = ""; string ddy = ""; string sj = ""; var temp1 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("志愿者")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp1.Count > 0) { zyz = temp1[0].SystemRoleUuid.ToString(); } var temp2 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("用户")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp2.Count > 0) { yh = temp2[0].SystemRoleUuid.ToString(); } var temp3 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("督导员")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp3.Count > 0) { ddy = temp3[0].SystemRoleUuid.ToString(); } var temp4 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("商")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp4.Count > 0) { sj = temp4[0].SystemRoleUuid.ToString(); } var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.LoginName), new Claim("guid", user.SystemUserUuid.ToString()), new Claim("avatar", ""), new Claim("displayName", user.RealName), new Claim("loginName", user.LoginName), new Claim("emailAddress", ""), //new Claim("guid",user.SystemUserUuid.ToString()), new Claim("userType", ((int)user.UserType).ToString()), new Claim("roleid", (user.SystemRoleUuid.TrimEnd(','))), new Claim("roleName", (rolename.TrimEnd(','))), new Claim("ZYZ", (zyz)), new Claim("YH", (yh)), new Claim("DDY", (ddy)), new Claim("SJ", (sj)) }); var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity); //查询当前登录用户拥有的权限集合(非超级管理员) // var sqlPermission = @"SELECT P.SystemPermissionUUID AS PermissionCode,P.ActionCode AS PermissionActionCode,P.Name AS PermissionName,P.Type AS PermissionType,M.Name AS MenuName,M.SystemMenuUUID AS MenuGuid,M.Alias AS MenuAlias,M.IsDefaultRouter FROM SystemRolePermissionMapping AS RPM //LEFT JOIN SystemPermission AS P ON P.SystemPermissionUUID = RPM.SystemPermissionUUID //INNER JOIN SystemMenu AS M ON M.SystemMenuUUID = P.SystemMenuUUID //WHERE P.IsDeleted=0 AND P.Status=1 AND EXISTS (SELECT 1 FROM SystemUserRoleMapping AS URM WHERE URM.SystemUserUUID={0} AND URM.SystemRoleUUID=RPM.SystemRoleUUID)"; // if (user.UserType == 0) // { // //如果是超级管理员 // sqlPermission = @"SELECT P.SystemPermissionUUID AS PermissionCode,P.ActionCode AS PermissionActionCode,P.Name AS PermissionName,P.Type AS PermissionType,M.Name AS MenuName,M.SystemMenuUUID AS MenuGuid,M.Alias AS MenuAlias,M.IsDefaultRouter FROM SystemPermission AS P //INNER JOIN SystemMenu AS M ON M.SystemMenuUUID = P.SystemMenuUUID //WHERE P.IsDeleted=0 AND P.Status=1"; // } // var permissions = _dbContext.SystemPermissionWithMenu.FromSql(sqlPermission, user.SystemUserUuid.ToString()).ToList(); // var pagePermissions = permissions.GroupBy(x => x.MenuAlias).ToDictionary(g => g.Key, g => g.Select(x => x.PermissionActionCode).Distinct()); response.SetData(new { access = new string[] { }, user_guid = user.SystemUserUuid, user_name = user.RealName, user_type = user.UserType, permissions = "null", roleName = GetroleName(user.SystemRoleUuid), address = user.Address, tokens = token, phone = user.Phone, shop_guid = user.ShopUuid, HomeAddressUUID = user.HomeAddressUuid, openid, idCard = user.UserIdCard, }); } return(Ok(response)); } }
public IActionResult Getuserinfo(string strlist) { var response = ResponseModelFactory.CreateResultInstance; using (_dbContext) { var code = strlist; //TODO:钉钉相关的配置信息都要放到配置文件中 string suiteKey = "dinga7xg5vjb2lwwvicu"; string suiteSecret = "pUiI0xvN0ZEbsFavSbuaLqctwHL2p9cIRlQ4HU5GS7y-TmYngcTjJGuI309ZLR_h"; string timestamp = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000).ToString(); string suiteTicket = "TestSuiteTicket"; string signature1 = timestamp + "\n" + suiteTicket; string signature2 = HmacSHA256(signature1, suiteSecret); string signature = System.Web.HttpUtility.UrlEncode(signature2, System.Text.Encoding.UTF8); string auth_corpid = strlist; string url = "https://oapi.dingtalk.com/gettoken?appkey=" + suiteKey + "&appsecret=" + suiteSecret; try { var response11 = Haikan3.Utils.DingDingHelper.HttpGet(url); var result = Newtonsoft.Json.JsonConvert.DeserializeObject <HaikanCRM.Api.ViewModels.DIngDing.PersistentCodeResult>(response11); if (result != null && result.errcode == "0") { string url11 = "https://oapi.dingtalk.com/user/getuserinfo?access_token=" + result.access_token + "&code=" + code; var response12 = Haikan3.Utils.DingDingHelper.HttpGet(url11); var result12 = Newtonsoft.Json.JsonConvert.DeserializeObject <HaikanCRM.Api.ViewModels.DIngDing.PersistentCodeResult12>(response12); if (result12 != null && result12.errcode == 0) { //获取人员信息 //var results = Haikan3.Utils.DingDingHelper.GetUserDetail(result.access_token, result12.userid); var roiduuid = _dbContext.SystemRole.FirstOrDefault(x => x.RoleName == "客户经理"); var userdata = _dbContext.SystemUser.Where(x => x.Streets == result12.userid).ToList().Count; if (userdata == 0) { UserEditViewModel model = new UserEditViewModel(); string pas = "******"; var entity = new HaikanCRM.Api.Entities.SystemUser(); entity.SystemUserUuid = Guid.NewGuid(); entity.Streets = result12.userid; entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); entity.RealName = result12.name; entity.LoginName = result12.name; //entity.PassWord = Haikan3.Utils.DesEncrypt.Encrypt(pas.Trim(), MdDesEncrypt.SecretKey); entity.PassWord = Security.GenerateMD5(pas.Trim()); entity.SystemRoleUuid = roiduuid.SystemRoleUuid.ToString(); entity.IsDeleted = 0; entity.ZaiGang = "在岗"; entity.UserType = 2; _dbContext.SystemUser.Add(entity); _dbContext.SaveChanges(); _dbContext.Database.ExecuteSqlRaw("DELETE FROM SystemUserRoleMapping WHERE SystemUserUUID={0}", entity.SystemUserUuid); var success = true; ////循环加权限 //for (int i = 0; i < model.SystemRoleUuid.Count; i++) //{ if (entity.SystemRoleUuid != null) { var roles = new SystemUserRoleMapping(); roles.SystemUserUuid = entity.SystemUserUuid; roles.SystemRoleUuid = Guid.Parse(entity.SystemRoleUuid); roles.AddTime = DateTime.Now.ToString("yyyy-MM-dd"); roles.AddPeople = AuthContextService.CurrentUser.DisplayName; _dbContext.SystemUserRoleMapping.Add(roles); } //} success = _dbContext.SaveChanges() > 0; if (success) { response.SetSuccess(); } else { _dbContext.Database.ExecuteSqlRaw("DELETE FROM SystemUser WHERE SystemUserUUID={0}", entity.SystemUserUuid); response.SetFailed("保存用户角色数据失败"); } } var user = _dbContext.SystemUser.FirstOrDefault(x => x.IsDeleted == 0 && x.Streets == result12.userid); var role = _dbContext.SystemRole.FirstOrDefault(x => x.SystemRoleUuid == Guid.Parse(user.SystemRoleUuid)); var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, result12.userid), new Claim("guid", user.SystemUserUuid.ToString()), new Claim("avatar", ""), new Claim("displayName", user.RealName), new Claim("loginName", user.LoginName), new Claim("emailAddress", ""), //new Claim("guid",user.SystemUserUuid.ToString()), //new Claim("userType",usertype.ToString()), new Claim("userType", user.UserType.Value.ToString()), new Claim("roleid", user.SystemRoleUuid.TrimEnd(',')), new Claim("roleName", role.RoleName.TrimEnd(',')), new Claim("ZYZ", ""), new Claim("YH", ""), new Claim("DDY", ""), new Claim("SJ", "") }); var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity); response.SetData(new { user, token }); return(Ok(response)); } } } catch (Exception ex) { throw new Exception(ex.Message); } } return(Ok(response)); }
public IActionResult receive(UserData userdata) { var response = ResponseModelFactory.CreateInstance; SystemUser user; using (_dbContext) { user = _dbContext.SystemUser.FirstOrDefault(x => x.LoginName == userdata.username.Trim()); if (user == null || user.IsDeleted == 1) { response.SetFailed("用户不存在"); return(Ok(response)); } else { string s = Haikan3.Utils.DesEncrypt.Encrypt(userdata.password.Trim(), MdDesEncrypt.SecretKey); if (user.PassWord != Haikan3.Utils.DesEncrypt.Encrypt(userdata.password.Trim(), MdDesEncrypt.SecretKey)) { response.SetFailed("密码不正确"); return(Ok(response)); } else { if (user.RealName != "超级管理员") { var dd = user.SystemRoleUuid; var ss = _dbContext.SystemRolePermissionMapping.Where(x => x.SystemRoleUuid.ToString() == dd).ToList(); if (ss.Count() > 0) { for (int i = 0; i < ss.Count(); i++) { var gg = _dbContext.SystemPermission.Where(x => x.SystemPermissionUuid == ss[i].SystemPermissionUuid).ToList(); if (gg.Count() > 0) { for (int r = 0; r < gg.Count(); r++) { if (gg[r].Name == "查看") { var qq = _dbContext.SystemMenu.FirstOrDefault(x => x.SystemMenuUuid == gg[r].SystemMenuUuid); if (userdata.jiaose == qq.Name) { //获取权限名 string[] roleid = user.SystemRoleUuid.TrimEnd(',').Split(","); string rolename = ""; for (int o = 0; o < roleid.Length; o++) { if (!string.IsNullOrEmpty(roleid[o])) { rolename += _dbContext.SystemRole.FirstOrDefault(x => x.SystemRoleUuid == Guid.Parse(roleid[o])).RoleName + ","; } } string zyz = ""; string yh = ""; string ddy = ""; string sj = ""; //志愿者roleid var temp1 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("志愿者")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp1.Count > 0) { zyz = temp1[0].SystemRoleUuid.ToString(); } //普通用户roleid var temp2 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("用户")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp2.Count > 0) { yh = temp2[0].SystemRoleUuid.ToString(); } //督导员roleid var temp3 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("督导员")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp3.Count > 0) { ddy = temp3[0].SystemRoleUuid.ToString(); } //商户 var temp4 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("商")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp4.Count > 0) { sj = temp4[0].SystemRoleUuid.ToString(); } string superAdmin = ""; //超管roleid var temp5 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("超级")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp5.Count > 0) { superAdmin = temp5[0].SystemRoleUuid.ToString(); } int usertype = 0; if (!user.SystemRoleUuid.Contains(superAdmin)) { usertype = 2; } var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, userdata.username), new Claim("guid", user.SystemUserUuid.ToString()), new Claim("avatar", ""), new Claim("displayName", user.RealName), new Claim("loginName", user.LoginName), new Claim("emailAddress", ""), //new Claim("guid",user.SystemUserUuid.ToString()), //new Claim("userType",usertype.ToString()), new Claim("department", user.DepartmentUuid.ToString()), new Claim("userType", user.UserType.Value.ToString()), new Claim("roleid", user.SystemRoleUuid.TrimEnd(',')), new Claim("roleName", rolename.TrimEnd(',')), new Claim("ZYZ", zyz), new Claim("YH", yh), new Claim("DDY", ddy), new Claim("SJ", sj) }); var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity); response.SetData(token); return(Ok(response)); } else { //response.SetFailed("没有相关权限"); //return Ok(response); } } else { //response.SetFailed("没有相关权限"); //return Ok(response); } } } else { //response.SetFailed("没有相关权限"); //return Ok(response); } } } else { response.SetFailed("没有相关权限"); return(Ok(response)); } } else { //获取权限名 string[] roleid = user.SystemRoleUuid.TrimEnd(',').Split(","); string rolename = ""; for (int o = 0; o < roleid.Length; o++) { if (!string.IsNullOrEmpty(roleid[o])) { rolename += _dbContext.SystemRole.FirstOrDefault(x => x.SystemRoleUuid == Guid.Parse(roleid[o])).RoleName + ","; } } string zyz = ""; string yh = ""; string ddy = ""; string sj = ""; //志愿者roleid var temp1 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("志愿者")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp1.Count > 0) { zyz = temp1[0].SystemRoleUuid.ToString(); } //普通用户roleid var temp2 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("用户")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp2.Count > 0) { yh = temp2[0].SystemRoleUuid.ToString(); } //督导员roleid var temp3 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("督导员")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp3.Count > 0) { ddy = temp3[0].SystemRoleUuid.ToString(); } //商户 var temp4 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("商")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp4.Count > 0) { sj = temp4[0].SystemRoleUuid.ToString(); } string superAdmin = ""; //超管roleid var temp5 = _dbContext.SystemRole.Where(x => x.RoleName.Contains("超级")).Select(x => new { x.SystemRoleUuid }).ToList(); if (temp5.Count > 0) { superAdmin = temp5[0].SystemRoleUuid.ToString(); } int usertype = 0; if (!user.SystemRoleUuid.Contains(superAdmin)) { usertype = 2; } var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, userdata.username), new Claim("guid", user.SystemUserUuid.ToString()), new Claim("avatar", ""), new Claim("displayName", user.RealName), new Claim("loginName", user.LoginName), new Claim("emailAddress", ""), //new Claim("guid",user.SystemUserUuid.ToString()), //new Claim("userType",usertype.ToString()), new Claim("department", user.DepartmentUuid.ToString()), new Claim("userType", user.UserType.Value.ToString()), new Claim("roleid", user.SystemRoleUuid.TrimEnd(',')), new Claim("roleName", rolename.TrimEnd(',')), new Claim("ZYZ", zyz), new Claim("YH", yh), new Claim("DDY", ddy), new Claim("SJ", sj) }); var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity); response.SetData(token); return(Ok(response)); } } response.SetFailed("没有相关权限"); return(Ok(response)); } } }
public IActionResult AutoCreate([FromBody] AutoCreateUserViewModel model, bool autoPassword = true, bool returnToken = false) { ResponseModel response = ResponseModelFactory.CreateInstance; //查找用户是否已经存在 DncUser target = _dbContext.DncUser.FirstOrDefault(x => (!string.IsNullOrWhiteSpace(model.WeChatUnionID) && x.WeChatUnionID == model.WeChatUnionID) || x.PhoneNumber == model.PhoneNumber); //不存在时自动创建 if (target == default) { UserCreateViewModel user = new UserCreateViewModel { LoginName = model.PhoneNumber ?? model.WeChatUnionID, DisplayName = model.PhoneNumber, PhoneNumber = model.PhoneNumber, IsLocked = IsLocked.UnLocked, Status = UserStatus.Normal, UserType = UserType.GeneralUser, IsDeleted = IsDeleted.No, }; if (autoPassword) { string password = model.PhoneNumber ?? DateTime.Now.ToString(); user.Password = model.PhoneNumber != null ? $"{user.LoginName}${PasswordCalculator.SaltPassword(user.LoginName, PasswordCalculator.Md5(password))}" : null; } Create(user); target = _dbContext.DncUser.FirstOrDefault(x => (!string.IsNullOrWhiteSpace(model.WeChatUnionID) && x.WeChatUnionID.Trim() == model.WeChatUnionID) || x.PhoneNumber.Trim() == model.PhoneNumber); } //依然为default则创建失败 if (target == default) { response.SetFailed("用户创建失败"); } else { //如果Permission不为空,则检查是否具有对应的权限位 if (!string.IsNullOrWhiteSpace(model.Permission)) { IQueryable <DncUserRoleMapping> targetPermission = from role in _dbContext.DncUserRoleMapping where role.UserGuid == target.Guid join permission in _dbContext.DncRolePermissionMapping on role.RoleCode equals permission.RoleCode where permission.PermissionCode == model.Permission select role; if (targetPermission.FirstOrDefault() == default) { _dbContext.DncUserRoleMapping.Add(new DncUserRoleMapping { CreatedOn = DateTime.Now, UserGuid = target.Guid, RoleCode = model.Permission }); _dbContext.SaveChanges(); } } //创建返回体 dynamic result = new ExpandoObject(); result.Guid = target.Guid; if (returnToken) { result.Authorization = TokenBulider(target); } response.SetData(result); } return(Ok(response)); string TokenBulider(DncUser user) { if (user == null || user.IsDeleted == IsDeleted.Yes) { throw new InvalidOperationException("用户不存在"); } if (user.IsLocked == IsLocked.Locked) { throw new InvalidOperationException("账号已被锁定"); } if (user.Status == UserStatus.Forbidden) { throw new InvalidOperationException("账号已被禁用"); } ClaimsIdentity claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.LoginName), new Claim("guid", user.Guid.ToString()), new Claim("avatar", ""), new Claim("displayName", user.DisplayName), new Claim("loginName", user.LoginName), new Claim("emailAddress", ""), new Claim("guid", user.Guid.ToString()), new Claim("userType", ((int)user.UserType).ToString()), }); return(JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity)); } }