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()); } }