FormsAuthenticationHelper.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Security;
  6. using System.Web;
  7. using System.Security.Principal;
  8. using System.Configuration;
  9. using System.Reflection;
  10. namespace Bowin.Common
  11. {
  12. public class FormsAuthenticationHelper
  13. {
  14. /// <summary>
  15. /// 创建登陆票据和存cookie
  16. /// 用于登陆成功后标记已登陆和用户信息
  17. /// </summary>
  18. /// <param name="httpContext">当前httpContext</param>
  19. /// <param name="cookieName">系统cookie名字</param>
  20. /// <param name="luserData">用户信息和CustomPrincipal对应</param>
  21. /// <returns></returns>
  22. public bool loginFormsAuthentication(HttpContextBase httpContext, string cookieName, List<string> luserData)
  23. {
  24. bool result = false;
  25. try
  26. {
  27. FormsAuthenticationTicket authTicket;
  28. authTicket = new FormsAuthenticationTicket(
  29. 1, //version
  30. luserData[0], //loginID
  31. DateTime.Now, // creation
  32. DateTime.Now.Add(FormsAuthentication.Timeout),//expired time
  33. false, //Persistent
  34. string.Join("|", luserData));
  35. //加密authTicket
  36. string data = FormsAuthentication.Encrypt(authTicket);
  37. HttpCookie cookies = new HttpCookie(cookieName, data);
  38. cookies.Domain = FormsAuthentication.CookieDomain;
  39. cookies.Secure = false;
  40. cookies.Path = FormsAuthentication.FormsCookiePath;
  41. cookies.Expires = authTicket.Expiration;
  42. httpContext.Response.Cookies.Add(cookies);
  43. result = true;
  44. }
  45. catch (Exception ex)
  46. {
  47. result = false;
  48. }
  49. return result;
  50. }
  51. /// <summary>
  52. /// 在Global.asax的Application_AuthenticateRequest事件使用
  53. /// 把客户端cockie解密存放于Page.User.(CustomPrincipal)对象
  54. /// </summary>
  55. /// <param name="httpContext">当前httpContext</param>
  56. /// <param name="cookieName">系统cookie名字</param>
  57. /// <param name="l_unvalidateUrl">排除掉需要验证登陆的url关健词</param>
  58. public void AuthenticateRequest(HttpContext httpContext, string cookieName, List<string> l_unvalidateUrl)
  59. {
  60. HttpCookie authCookie = httpContext.Request.Cookies[cookieName];
  61. if (authCookie == null)
  62. {
  63. string rurl = httpContext.Request.Path.ToLower();
  64. l_unvalidateUrl.Add(FormsAuthentication.LoginUrl.TrimStart('~'));
  65. if (l_unvalidateUrl.Where(w => rurl.IndexOf(w.ToLower()) >= 0).Count() == 0)
  66. {
  67. httpContext.Response.Redirect(FormsAuthentication.LoginUrl + "?targetUrl=" + httpContext.Server.UrlEncode(httpContext.Request.Url.AbsoluteUri));
  68. }
  69. return;
  70. }
  71. FormsAuthenticationTicket authTicket = null; //票椐
  72. try
  73. {
  74. authTicket = System.Web.Security.FormsAuthentication.Decrypt(authCookie.Value); // Cookie 解密
  75. }
  76. catch (Exception ex)
  77. {
  78. //cookie解密报错清除
  79. authCookie.Expires = DateTime.Now.AddDays(-1);
  80. httpContext.Response.Cookies.Add(authCookie);
  81. httpContext.Response.Redirect(FormsAuthentication.LoginUrl);
  82. return;
  83. }
  84. string UserName = authTicket.UserData; //分解存在Cookie内的用户信息--
  85. FormsIdentity id = new FormsIdentity(authTicket);
  86. ICustomPrincipal principal = CustomPrincipalFactory.Create(id, UserName);
  87. httpContext.User = principal;
  88. }
  89. public void GetTicket(HttpContext httpContext, string url)
  90. {
  91. try
  92. {
  93. System.Web.Security.FormsAuthenticationTicket tk = ((System.Web.Security.FormsIdentity)httpContext.User.Identity).Ticket;
  94. string key = System.Web.Security.FormsAuthentication.Encrypt(tk); //每次加密后的字串都是不同的
  95. httpContext.Response.Redirect(url + "?ssologin=1&ticket=" + key); //跳转至下一页面
  96. }
  97. catch
  98. {
  99. httpContext.Response.Redirect(url + "?ssologin=0"); //跳转至下一页面
  100. }
  101. finally
  102. {
  103. httpContext.Response.End();
  104. }
  105. }
  106. public ICustomPrincipal ValidateTitcket(HttpContext httpContext, string ticket)
  107. {
  108. FormsAuthenticationTicket authTicket = null; //票椐
  109. try
  110. {
  111. authTicket = System.Web.Security.FormsAuthentication.Decrypt(ticket); // Cookie 解密
  112. }
  113. catch (Exception ex)
  114. {
  115. return null;
  116. }
  117. string UserName = authTicket.UserData; //分解存在Cookie内的用户信息--
  118. FormsIdentity id = new FormsIdentity(authTicket);
  119. ICustomPrincipal principal = CustomPrincipalFactory.Create(id, UserName);
  120. return principal;
  121. }
  122. }
  123. public interface ICustomPrincipal : IPrincipal
  124. {
  125. Guid UserID { get; set; }
  126. string LoginID { get; set; }
  127. string Name { get; set; }
  128. void ProcessData(IIdentity id, string data);
  129. }
  130. public class CustomPrincipalFactory
  131. {
  132. public static ICustomPrincipal Create(IIdentity id, string data)
  133. {
  134. string assemblyName = ConfigurationManager.AppSettings["CustomPrincipanAssembly"];
  135. string className = ConfigurationManager.AppSettings["CustomPrincipanClass"];
  136. Assembly assembly = Assembly.Load(assemblyName);
  137. ICustomPrincipal principal = (ICustomPrincipal)assembly.CreateInstance(className);
  138. principal.ProcessData(id, data);
  139. return principal;
  140. }
  141. }
  142. }