/// <summary> /// 验证用户 /// </summary> /// <param name="companyName">公司</param> /// <param name="userName">用户名</param> /// <param name="password">密码</param> /// <param name="openId">OpenId</param> /// <param name="permissionCode">权限编号</param> /// <param name="ipAddress">IP地址</param> /// <param name="systemCode"></param> /// <param name="persistCookie">是否保存密码</param> /// <param name="formsAuthentication">表单验证,是否需要重定位</param> /// <returns></returns> public static BaseUserInfo LogonByCompany(string companyName, string userName, string password, string openId, string permissionCode, string ipAddress, string systemCode, bool persistCookie, bool formsAuthentication) { var taskId = Guid.NewGuid().ToString("N"); // 统一的登录服务 var userInfo = GetUserInfo(); if (!string.IsNullOrEmpty(ipAddress)) { userInfo.IpAddress = ipAddress; } if (!string.IsNullOrEmpty(systemCode)) { userInfo.SystemCode = systemCode; } if (!string.IsNullOrEmpty(userInfo.IpAddress)) { userInfo.IpAddress = Utils.GetIp(); } var dotNetService = new DotNetService(); var userLogonResult = dotNetService.LogonService.LogonByCompany(taskId, userInfo, companyName, userName, password, openId); // 检查身份 if (userLogonResult.Status == Status.Ok) { var isAuthorized = true; // 用户是否有哪个相应的权限 if (!string.IsNullOrEmpty(permissionCode)) { isAuthorized = dotNetService.PermissionService.IsAuthorized(userInfo, permissionCode, null); } // 有相应的权限才可以登录 if (isAuthorized) { if (persistCookie) { // 相对安全的方式保存登录状态 SaveCookie(userName, password); // 内部单点登录方式 //SaveCookie(userLogonResult.UserInfo); } else { RemoveUserCookie(); } Logon(userLogonResult.UserInfo, formsAuthentication); } else { userLogonResult.Status = Status.LogonDeny; userLogonResult.StatusCode = Status.LogonDeny.ToString(); userLogonResult.StatusMessage = "访问被拒绝、您的账户没有后台管理访问权限。"; } } return(userLogonResult.UserInfo); }
/// <summary> /// 获取中通单号规则 /// </summary> /// <param name="fromDbConfigure">是否从数据库里获取</param> /// <param name="internalSystem">是否从内部系统获取</param> /// <returns>单号规则</returns> public static string GetBillCodeRegex(bool fromDbConfigure = true, bool internalSystem = false, string hots = "https://userCenter.zt-express.com") { string billRule = string.Empty; string cacheObject = "BillRule"; // 从数据库配置读取 if (fromDbConfigure) { System.Web.Caching.Cache cache = HttpRuntime.Cache; if (cache != null && cache[cacheObject] == null) { lock (BaseSystemInfo.UserLock) { if (cache != null && cache[cacheObject] == null) { // 是内部系统 if (internalSystem) { // 通过WCF或者本地模式获取 DotNetService dotNetService = new DotNetService(); billRule = dotNetService.ParameterService.GetParameter(BaseSystemInfo.UserInfo, "SystemParameter", "System", "ZTO", "AllBillRule"); if (dotNetService.ParameterService is ICommunicationObject) { ((ICommunicationObject)dotNetService.ParameterService).Close(); } } else { // 通过BS远程获取单号规则参数获取 if (string.IsNullOrWhiteSpace(hots)) { hots = "https://userCenter.zt-express.com/"; } string url = hots + "/UserCenterV42/ParameterService.ashx?function=GetParameter&tableName=SystemParameter&CategoryCode=System&ParameterId=ZTO&ParameterCode=AllBillRule"; billRule = DotNet.Business.Utilities.GetResponse(url); } if (string.IsNullOrEmpty(billRule)) { billRule = "^((768|778|828|618|680|518|688|010|880|660|805|988|628|205|717|718|728|761|762|701|757|751|358|100|200|128|689)[0-9]{9})$|^((5711|2008|2009|2010)[0-9]{8})$|^((8010|8021)[0-9]{6})$|^([0-9a-zA-Z]{12})$|^(1111[0-9]{10})$|^((a|b|h)[0-9]{13})$|^((9|90|10|19)[0-9]{12})$|^((5)[0-9]{9})$|^(50|51)[0-9]{11}$|^[A-Z]{2}[0-9]{9}[A-Z]{2}$|^[0-9]{13}$|^((88|89|91|92|93|94|95|96|99)[0-9]{8})$|^((8|9)[0-9]{7})$|^((90|36|68)[0-9]{10})$"; } cache.Add(cacheObject, billRule, null, DateTime.Now.AddHours(8), TimeSpan.Zero, CacheItemPriority.Normal, null); } } } billRule = cache[cacheObject] as string; } if (string.IsNullOrEmpty(billRule)) { billRule = "^((768|778|828|618|680|518|688|010|880|660|805|988|628|205|717|718|728|761|762|701|757|751|358|100|200|128|689)[0-9]{9})$|^((5711|2008|2009|2010)[0-9]{8})$|^((8010|8021)[0-9]{6})$|^([0-9a-zA-Z]{12})$|^(1111[0-9]{10})$|^((a|b|h)[0-9]{13})$|^((9|90|10|19)[0-9]{12})$|^((5)[0-9]{9})$|^(50|51)[0-9]{11}$|^[A-Z]{2}[0-9]{9}[A-Z]{2}$|^[0-9]{13}$|^((88|89|91|92|93|94|95|96|99)[0-9]{8})$|^((8|9)[0-9]{7})$|^((90|36|68)[0-9]{10})$"; } return(billRule); }
//LDAP域用户登录部分:包括Windows AD域用户登录 #region public static BaseUserInfo LogonByLDAP(string domain, string lDAP, string userName, string password, string permissionCode, bool persistCookie, bool formsAuthentication, out Status status, out string statusMessage) /// <summary> /// 验证LDAP用户 /// </summary> /// <param name="domain">域</param> /// <param name="lDap">LDAP</param> /// <param name="systemCode">子系统</param> /// <param name="userName">域用户名</param> /// <param name="password">域密码</param> /// <param name="openId">OpenId</param> /// <param name="permissionCode">权限编号</param> /// <param name="persistCookie">是否保存密码</param> /// <param name="formsAuthentication">表单验证,是否需要重定位</param> /// <param name="status">状态</param> /// <param name="statusMessage"></param> /// <returns></returns> public static BaseUserInfo LogonByLdap(string domain, string lDap, string systemCode, string userName, string password, string openId, string permissionCode, bool persistCookie, bool formsAuthentication, out Status status, out string statusMessage) { BaseUserInfo baseUserInfo = null; // 统一的登录服务 var taskId = Guid.NewGuid().ToString("N"); var userInfo = GetUserInfo(); if (string.IsNullOrEmpty(systemCode)) { systemCode = BaseSystemInfo.SystemCode; if (string.IsNullOrEmpty(systemCode)) { systemCode = userInfo.SystemCode; } } if (string.IsNullOrEmpty(userInfo.IpAddress)) { userInfo.IpAddress = Utils.GetIp(); } status = Status.UserNotFound; statusMessage = Status.UserNotFound.ToDescription(); var dirEntry = new DirectoryEntry(); dirEntry.Path = lDap; dirEntry.Username = domain + "\\" + userName; dirEntry.Password = password; dirEntry.AuthenticationType = AuthenticationTypes.Secure; try { var dirSearcher = new DirectorySearcher(dirEntry); dirSearcher.Filter = String.Format("(&(&(objectClass=user))(samAccountName={0}))", userName); var result = dirSearcher.FindOne(); //如果LDAP用户登录验证通过 if (result != null) { // 统一的登录服务 var dotNetService = new DotNetService(); var userLogonResult = dotNetService.LogonService.LogonByUserName(taskId, systemCode, GetUserInfo(), userName); // 检查身份 if (userLogonResult.Status == Status.Ok) { var isAuthorized = true; // 用户是否有哪个相应的权限 if (!string.IsNullOrEmpty(permissionCode)) { isAuthorized = dotNetService.PermissionService.IsAuthorized(userInfo, permissionCode, null); } // 有相应的权限才可以登录 if (isAuthorized) { if (persistCookie) { // 相对安全的方式保存登录状态 //SaveCookie(userName, password); // 内部单点登录方式 Troy.Cui 2016.12.26 SaveCookie(userInfo); } else { RemoveUserCookie(); } Logon(userLogonResult.UserInfo, formsAuthentication); } else { userLogonResult.Status = Status.LogonDeny; userLogonResult.StatusCode = Status.LogonDeny.ToString(); userLogonResult.StatusMessage = "访问被拒绝、您的账户没有后台管理访问权限。"; status = Status.LogonDeny; statusMessage = "访问被拒绝、您的账户没有后台管理访问权限。"; baseUserInfo = userLogonResult.UserInfo; } userLogonResult.Status = Status.Ok; userLogonResult.StatusCode = Status.Ok.ToString(); userLogonResult.StatusMessage = "登录成功"; status = Status.Ok; statusMessage = "登录成功"; baseUserInfo = userLogonResult.UserInfo; } else { status = Status.LogonDeny; statusMessage = "应用系统用户不存在,请联系管理员。"; } } } catch (Exception e) { //Logon failure: unknown user name or bad password. status = Status.LogonDeny; statusMessage = "域服务器返回信息" + e.Message.Replace("\r\n", ""); } return(baseUserInfo); }
/// <summary> /// LogonWindowsAuthentication /// </summary> /// <param name="systemCode">子系统</param> /// <param name="userName">域用户名</param> /// <param name="permissionCode">权限编号</param> /// <param name="persistCookie">是否保存密码</param> /// <param name="formsAuthentication">表单验证,是否需要重定位</param> /// <param name="status">状态</param> /// <param name="statusMessage"></param> /// <returns></returns> public static BaseUserInfo LogonWindowsAuthentication(string systemCode, string userName, string permissionCode, bool persistCookie, bool formsAuthentication, out Status status, out string statusMessage) { BaseUserInfo baseUserInfo = null; // 统一的登录服务 var taskId = Guid.NewGuid().ToString("N"); var userInfo = GetUserInfo(); if (string.IsNullOrEmpty(systemCode)) { systemCode = BaseSystemInfo.SystemCode; if (string.IsNullOrEmpty(systemCode)) { systemCode = userInfo.SystemCode; } } if (string.IsNullOrEmpty(userInfo.IpAddress)) { userInfo.IpAddress = Utils.GetIp(); } // 统一的登录服务 var dotNetService = new DotNetService(); var userLogonResult = dotNetService.LogonService.LogonByUserName(taskId, systemCode, GetUserInfo(), userName); // 检查身份 if (userLogonResult.Status == Status.Ok) { var isAuthorized = true; // 用户是否有哪个相应的权限 if (!string.IsNullOrEmpty(permissionCode)) { isAuthorized = dotNetService.PermissionService.IsAuthorized(userInfo, permissionCode, null); } // 有相应的权限才可以登录 if (isAuthorized) { if (persistCookie) { // 相对安全的方式保存登录状态 //SaveCookie(userName, password); // 内部单点登录方式 Troy.Cui 2016.12.26 SaveCookie(userInfo); } else { RemoveUserCookie(); } Logon(userLogonResult.UserInfo, formsAuthentication); userLogonResult.Status = Status.Ok; userLogonResult.StatusCode = Status.Ok.ToString(); userLogonResult.StatusMessage = "登录成功"; status = Status.Ok; statusMessage = "登录成功"; baseUserInfo = userLogonResult.UserInfo; } else { userLogonResult.Status = Status.LogonDeny; userLogonResult.StatusCode = Status.LogonDeny.ToString(); userLogonResult.StatusMessage = "访问被拒绝、您的账户没有访问权限。"; status = Status.LogonDeny; statusMessage = "访问被拒绝、您的账户没有访问权限。"; baseUserInfo = userLogonResult.UserInfo; } } else { userLogonResult.Status = Status.LogonDeny; userLogonResult.StatusCode = Status.LogonDeny.ToString(); userLogonResult.StatusMessage = "访问被拒绝、您的账户没有访问权限。"; status = Status.LogonDeny; statusMessage = "访问被拒绝、您的账户没有访问权限。"; baseUserInfo = userLogonResult.UserInfo; } return(baseUserInfo); }
/// <summary> /// 验证用户 /// </summary> /// <param name="userName">用户名</param> /// <param name="password">密码</param> /// <param name="openId">单点登录标识openId</param> /// <param name="permissionCode">权限编号</param> /// <param name="ipAddress"></param> /// <param name="systemCode"></param> /// <param name="persistCookie">是否保存密码</param> /// <param name="formsAuthentication">表单验证,是否需要重定位</param> /// <param name="webApiLogin">是否WebApi登录,解决同一请求的Cookie清除无效问题</param> /// <param name="status">状态</param> /// <param name="statusMessage">返回状态消息</param> /// <returns></returns> public static BaseUserInfo Logon(string userName, string password, string openId, string permissionCode, string ipAddress, string systemCode, bool persistCookie, bool formsAuthentication, bool webApiLogin, out Status status, out string statusMessage) { BaseUserInfo result = null; status = Status.UserNotFound; statusMessage = Status.UserNotFound.ToDescription(); // 统一的登录服务 var taskId = Guid.NewGuid().ToString("N"); var dotNetService = new DotNetService(); var userInfo = GetUserInfo(); if (!string.IsNullOrEmpty(ipAddress)) { userInfo.IpAddress = ipAddress; } if (!string.IsNullOrEmpty(systemCode)) { userInfo.SystemCode = systemCode; } if (string.IsNullOrEmpty(userInfo.IpAddress)) { userInfo.IpAddress = Utils.GetIp(); } //2020-06-12 WebApi中登录方法中无法先删除Cookie,因为没有返回给客户端。Troy.Cui if (webApiLogin) { userInfo = new BaseUserInfo { IpAddress = Utils.GetIp() }; } //2020年2月29日,每次登录都强制重新生成OpenId,Troy.Cui var userLogonResult = dotNetService.LogonService.UserLogon(taskId, userInfo, userName, password, openId); if (userLogonResult != null) { status = userLogonResult.Status; statusMessage = userLogonResult.StatusMessage; } // 检查身份 if (userLogonResult != null && userLogonResult.Status == Status.Ok) { //LogUtil.WriteLog("Logon Ok"); var isAuthorized = true; // 用户是否有哪个相应的权限 if (!string.IsNullOrEmpty(permissionCode)) { isAuthorized = dotNetService.PermissionService.IsAuthorized(userLogonResult.UserInfo, permissionCode, null); } // 有相应的权限才可以登录 if (isAuthorized) { if (persistCookie) { // 相对安全的方式保存登录状态 //SaveCookie(userName, password); // 内部单点登录方式 Troy.Cui 2016.12.26 SaveCookie(userLogonResult.UserInfo); } else { RemoveUserCookie(); } Logon(userLogonResult.UserInfo, formsAuthentication); } else { userLogonResult.Status = Status.LogonDeny; userLogonResult.StatusCode = Status.LogonDeny.ToString(); userLogonResult.StatusMessage = "访问被拒绝、您的账户没有后台管理访问权限。"; } result = userLogonResult.UserInfo; } return(result); }
// LDAP域用户登录部分:包括Windows AD域用户登录 #region public static BaseUserInfo LogOnByLDAP(string domain, string lDAP, string userName, string password, string permissionCode, bool persistCookie, bool formsAuthentication, out string statusCode, out string statusMessage) /// <summary> /// 验证LDAP用户 /// </summary> /// <param name="domain">域</param> /// <param name="lDAP">LDAP</param> /// <param name="userName">域用户名</param> /// <param name="password">域密码</param> /// <param name="permissionCode">权限编号</param> /// <param name="persistCookie">是否保存密码</param> /// <param name="formsAuthentication">表单验证,是否需要重定位</param> /// <param name="statusCode"></param> /// <param name="statusMessage"></param> /// <returns></returns> public static BaseUserInfo LogOnByLDAP(string domain, string lDAP, string userName, string password, string openId, string permissionCode, bool persistCookie, bool formsAuthentication, out string statusCode, out string statusMessage) { DirectoryEntry dirEntry = new DirectoryEntry(); dirEntry.Path = lDAP; dirEntry.Username = domain + "\\" + userName; dirEntry.Password = password; dirEntry.AuthenticationType = AuthenticationTypes.Secure; try { DirectorySearcher dirSearcher = new DirectorySearcher(dirEntry); dirSearcher.Filter = String.Format("(&(objectClass=user)(samAccountName={0}))", userName); System.DirectoryServices.SearchResult result = dirSearcher.FindOne(); if (result != null) { // 统一的登录服务 DotNetService dotNetService = new DotNetService(); BaseUserInfo userInfo = dotNetService.LogOnService.LogOnByUserName(Utilities.GetUserInfo(), userName, out statusCode, out statusMessage); //BaseUserInfo userInfo = dotNetService.LogOnService.UserLogOn(Utilities.GetUserInfo(), userName, password, openId, false, out statusCode, out statusMessage); // 检查身份 if (statusCode.Equals(Status.OK.ToString())) { userInfo.IPAddress = GetIPAddress(); bool isAuthorized = true; // 用户是否有哪个相应的权限 if (!string.IsNullOrEmpty(permissionCode)) { isAuthorized = dotNetService.PermissionService.IsAuthorized(userInfo, permissionCode, null); } // 有相应的权限才可以登录 if (isAuthorized) { if (persistCookie) { // 相对安全的方式保存登录状态 // SaveCookie(userName, password); // 内部单点登录方式 SaveCookie(userInfo); } else { RemoveUserCookie(); } LogOn(userInfo, formsAuthentication); } else { statusCode = Status.LogOnDeny.ToString(); statusMessage = "访问被拒绝、您的账户没有后台管理访问权限。"; } } return(userInfo); } else { statusCode = Status.LogOnDeny.ToString(); statusMessage = "应用系统用户不存在,请联系管理员。"; return(null); } } catch (Exception e) { //Logon failure: unknown user name or bad password. statusCode = Status.LogOnDeny.ToString(); statusMessage = "域服务器返回信息" + e.Message.Replace("\r\n", ""); return(null); } }