说明
上下文是系统级别的属性,用户的登录决定了后面所有操作依赖的环境,比如账套,客户端,权限等,所以一般是通过创建新的上下文来改变依赖的环境,而不是修改原来的上下文。
插件中创建上下文
方式1、使用数据中心服务得到上下文
using Kingdee.BOS.ServiceHelper; var ctx = DataCenterService.GetDataCenterContextByID("数据库账套id"); context.UserName = "用户名";
方式2、复制插件中的上下文
var ctx = ObjectUtils.CreateCopy(this.Context) as Context;
webapi自定义接口得到上下文
方式1、自带的上下文,需要先调用登录接口,否则上下文为null
var ctx = this.KDContext.Session.AppContext;
方式2、使用用户服务代理来创建上下文
using Kingdee.BOS.Authentication; using Kingdee.BOS.ServiceFacade.KDServiceClient.User; UserServiceProxy proxy = new UserServiceProxy(); string url = "http://localhost:2800/K3Cloud/"; LoginInfo loginInfo = new LoginInfo(); loginInfo.AcctID = "数据库账套id"; loginInfo.Username = "用户名"; loginInfo.Password = "用户密码"; loginInfo.Lcid = 2052; proxy.HostURL = url; var ctx = proxy.ValidateUser(url, loginInfo).Context;
方式3、使用用户服务创建上下文
using Kingdee.BOS.Authentication; using Kingdee.BOS.ServiceFacade.ServicesStub.User; LoginInfo loginInfo = new LoginInfo(); loginInfo.AcctID = "数据库账套id"; loginInfo.Username = "用户名"; loginInfo.Password = "用户密码"; loginInfo.Lcid = 2052; loginInfo.PasswordIsEncrypted = false; loginInfo.AuthenticateType = AuthenticationType.PwdAuthentication; UserService userService = new UserService(this.KDContext); var ctx = userService.ValidateLoginInfo(string.Empty, loginInfo).Context;
方式4、使用webapi验证服务创建上下文
using using Kingdee.BOS.WebApi.ServicesStub; AuthService authService = new AuthService(this.KDContext); var ctx = authService.ValidateUserByOrgNumber("数据库账套id", "用户名", "用户密码").Context;
复制上下文
var cloneCtx= ObjectUtils.CreateCopy(sContext) as Context;
cloneCtx.UserId = userId;
cloneCtx.ClientInfo = sContext.ClientInfo;
cloneCtx.CharacterSet = sContext.CharacterSet;
cloneCtx.IsStartTimeZoneTransfer = sContext.IsStartTimeZoneTransfer;
cloneCtx.UserAuthenticationMethod = sContext.UserAuthenticationMethod;
cloneCtx.UserLoginType = sContext.UserLoginType;
cloneCtx.UserX509Certificate = sContext.UserX509Certificate;
cloneCtx.TransIsolationLevel = sContext.TransIsolationLevel;
cloneCtx.ConsolePageId = sContext.ConsolePageId;
cloneCtx.CurrentServerName = sContext.CurrentServerName;
cloneCtx.ModuleName = sContext.ModuleName;
cloneCtx.CallerName = sContext.CallerName;
cloneCtx.CallStack = sContext.CallStack;
cloneCtx.KDPassportId = sContext.KDPassportId;
cloneCtx.GateWayIp = sContext.GateWayIp;
cloneCtx.CreateContextGuid = sContext.CreateContextGuid;
cloneCtx.NetCtrlMonitorIDForDataCenterM = sContext.NetCtrlMonitorIDForDataCenterM;
推荐阅读