private static void ProcessError(SysWorkflowMessage msg, Exception ex, string connectionStringOrName) { if (string.IsNullOrWhiteSpace(connectionStringOrName)) { connectionStringOrName = DataContext.BizConnectionStringDefault; } try { using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress)) { using (BizDataContext context = new BizDataContext(connectionStringOrName, true)) { if (msg.State != SysWorkflowMessageStateEnum.Running) { throw new Exception(string.Format("消息ID:{0}的状态不为Running", msg.MessageId)); } msg.State = SysWorkflowMessageStateEnum.Error; msg.LastErrorMessage = ex.Message; string sql = string.Format("UPDATE SysWorkflowMessage SET STATE = {0}, LastErrorMessage = {1} WHERE MessageId = {2} AND (STATE = {3} or STATE = {4}) ", new object[] { context.AddPrefixToParameterName("NewState"), context.AddPrefixToParameterName("LastErrorMessage"), context.AddPrefixToParameterName("MessageId"), context.AddPrefixToParameterName("OldState1"), context.AddPrefixToParameterName("OldState2") }); if (context.ExecuteNonQuery(sql, new DbParameter[] { context.CreateParameterWithPrefix("NewState", Convert.ToInt32(SysWorkflowMessageStateEnum.Error), typeof(int)), context.CreateParameterWithPrefix("LastErrorMessage", ex.Message, typeof(string)), context.CreateParameterWithPrefix("MessageId", msg.MessageId, typeof(int)), context.CreateParameterWithPrefix("OldState1", Convert.ToInt32(SysWorkflowMessageStateEnum.Running), typeof(int)), context.CreateParameterWithPrefix("OldState2", Convert.ToInt32(SysWorkflowMessageStateEnum.Inited), typeof(int)) }) <= 0) { AppLogHelper.Information("将工作流消息更改为错误状态时更新行数为0: MessageId={0}", new object[] { msg.MessageId }); } } scope.Complete(); } Console.WriteLine(ex.Message); } catch (Exception exception) { AppLogHelper.Error(exception, "将工作流消息更改为错误状态时失败: MessageId={0}", new object[] { msg.MessageId }); Console.WriteLine(exception.Message); } }
private void BuildSsoAuthenticate(IOwinContext context) { string ssoToken = GetSsoToken(context); this.LogSsoToken(context, ssoToken); if (!string.IsNullOrWhiteSpace(ssoToken)) { SysUser userFromToken; UserManager <SysUser> userManager = new UserManager <SysUser>(new UserStore()); this.TimeExpand(context, userManager); try { userFromToken = LoginUserHelper.GetUserFromToken(ssoToken); } catch (Exception exception) { AppLogHelper.Information(exception.ToString()); userFromToken = null; } if (userFromToken != null) { IAuthenticationManager manager2 = context.Authentication; manager2.SignOut("ExternalCookie"); ClaimsIdentity identity = userManager.CreateIdentity(userFromToken, "ApplicationCookie"); identity.AddUserData(userFromToken); AuthenticationProperties properties = new AuthenticationProperties { IsPersistent = true }; manager2.SignIn(properties, identity); try { Trace.WriteLine(new StringBuilder().Append(" find user: url = ").Append(context.Request.Uri.AbsolutePath).Append("; user_id = ").Append(identity.GetUserId()).Append("; user_name = ").Append(identity.GetUserName())); } catch { // ignored } ClaimsPrincipal principal = new ClaimsPrincipal(identity); context.Request.Set("User", principal); } } }
private void TimeExpand(IOwinContext context, UserManager <SysUser, string> userManager) { try { if (context.Authentication.User != null) { ClaimsIdentity identity = context.Authentication.User.Identity as ClaimsIdentity; if (identity != null) { SysUser userData = identity.GetUserData(); if ((userData != null) && !string.IsNullOrWhiteSpace(userData.SsoToken)) { DateTime now = DateTime.Now; DateTime?lastTokenBeatTime = userData.LastTokenBeatTime; DateTime time2 = lastTokenBeatTime.HasValue ? lastTokenBeatTime.GetValueOrDefault() : DateTime.Now.AddHours(-1.0); TimeSpan span = now - time2; if (span.TotalMinutes > max_ticks) { string accentUrl = LoginUserHelper.AppendTokenToUrl(LoginUserHelper.TokenValidateUrl, userData); string errMsg = string.Empty; if (!LoginUserHelper.ExtSsoTime(accentUrl, userData.SsoToken, out errMsg)) { AppLogHelper.Information(errMsg); } IAuthenticationManager manager = context.Authentication; manager.SignOut("ExternalCookie"); ClaimsIdentity identity2 = userManager.CreateIdentity(userData, "ApplicationCookie"); identity2.AddUserData(userData); AuthenticationProperties properties = new AuthenticationProperties { IsPersistent = true }; manager.SignIn(properties, identity2); } } } } } catch (Exception exception) { AppLogHelper.Information(exception.ToString()); } }
public void ResetMessageState(int messageId, int operationUserId) { int num; DataContext context = this.GetContext(); try { string sql = string.Format("UPDATE SysWorkflowMessage SET STATE = {0} WHERE MESSAGEID = {1} AND STATE = {2}", Convert.ToInt32(SysWorkflowMessageStateEnum.Inited), messageId, Convert.ToInt32(SysWorkflowMessageStateEnum.Error)); num = context.ExecuteNonQuery(sql, new DbParameter[0]); if (num > 0) { AppLogHelper.Information("User({0})更新工作流消息({1})的状态,从Error->Inited", new object[] { operationUserId, messageId }); } } finally { this.ReleaseContext(context); } if (num <= 0) { throw new ApplicationException("该消息不存在或已变更。"); } }
public static SysUser GetUserFromToken(string token) { SysUser user; if (IsSsoServer) { AppLogHelper.Information("IsSsoServer=true"); var userIdFromToken = GetUserIdFromToken(token); if (userIdFromToken <= 0) { return(null); } var manager = new UserManager <SysUser>(new UserStore()); user = manager.FindById(userIdFromToken.ToString()); user.SsoToken = token; user.LastTokenBeatTime = DateTime.Now; return(user); } if (!IsSsoClient) { return(null); } string str2; AppLogHelper.Information("IsSsoClient=true"); var address = AppendTokenToUrl(TokenValidateUrl, token); var client = new WebClient(); try { var tokenKey = TokenKey; var data = new NameValueCollection { { tokenKey, token } }; var bytes = client.UploadValues(address, data); if (bytes.Length > 0) { str2 = Encoding.UTF8.GetString(bytes); AppLogHelper.Information("token验证返回信息:" + str2); } else { AppLogHelper.Information("token验证时返回信息长度为0"); str2 = string.Empty; } } catch (Exception exception) { AppLogHelper.Error(exception); str2 = string.Empty; } finally { if (client != null) { client.Dispose(); } } if (!string.IsNullOrWhiteSpace(str2)) { user = JsonConvert.DeserializeObject <SysUser>(str2); user.SsoToken = token; user.LastTokenBeatTime = DateTime.Now; return(user); } return(null); }
public static void HandleMessage(SysWorkflowMessage msg, string connectionStringOrName) { if (string.IsNullOrWhiteSpace(connectionStringOrName)) { connectionStringOrName = DataContext.BizConnectionStringDefault; } try { Console.WriteLine("开始处理消息ID:{0},类型:{1}", msg.MessageId, msg.MessageType); using (TransactionScope scope = new TransactionScope()) { using (BizDataContext context = new BizDataContext(connectionStringOrName, true)) { if (LockMessage(msg, context)) { try { ProcessInstanceCacheFactory runtimeContext = new ProcessInstanceCacheFactory(context); SysProcessInstance processInstance = null; SysWorkItem item = null; switch (msg.MessageType) { case WorkflowMessageTypeEnum.StartingProcess: processInstance = runtimeContext.GetProcessInstanceCache(msg.ProcessInstanceId); break; case WorkflowMessageTypeEnum.ExecutingActivity: throw new Exception(string.Format("暂时不支持ExecutingActivity类型的消息", new object[0])); case WorkflowMessageTypeEnum.CompletingWorkItem: case WorkflowMessageTypeEnum.CompletingApproveWorkItem: processInstance = runtimeContext.GetProcessInstanceCacheByWorkItem(msg.WorkItemId, out item); break; default: throw new Exception(string.Format(string.Format("暂时不支持{0}类型的消息", msg.MessageType), new object[0])); } ProcessEngine engine = new ProcessEngine(runtimeContext, processInstance, item); msg.MessageType.CreateHandler(engine).ProcessMessage(msg); if (!CompleteMessage(msg, context)) { AppLogHelper.Information("完成工作流消息时更新行数为0: MessageId={0}", new object[] { msg.MessageId }); } scope.Complete(); return; } catch (Exception exception) { AppLogHelper.Error(exception, "处理工作流消息失败1: MessageId={0}", new object[] { msg.MessageId }); throw new MessageHandleException(exception); } } AppLogHelper.Information("锁定工作流消息时更新行数为0: MessageId={0}", new object[] { msg.MessageId }); } } } catch (MessageHandleException exception2) { ProcessError(msg, exception2.InnerException, connectionStringOrName); } catch (Exception exception3) { AppLogHelper.Error(exception3, "处理工作流消息失败2: MessageId={0}", new object[] { msg.MessageId }); ProcessError(msg, exception3, connectionStringOrName); } }