169 lines
6.3 KiB
C#
169 lines
6.3 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Web;
|
||
using System.Web.Security;
|
||
|
||
namespace Mtxfw.Utility
|
||
{
|
||
/* 2011.2.27 by Telesa */
|
||
|
||
/// <summary>
|
||
/// FromsAuthentication 身份验证类
|
||
/// </summary>
|
||
public class Authentication
|
||
{
|
||
#region 私有成员
|
||
private bool _IsAuthenticated = false;
|
||
private string _FormsCookieName = string.Empty;
|
||
|
||
#endregion
|
||
|
||
#region 公共属性
|
||
/// <summary>
|
||
/// 获取一个值 指示是否验证了请求
|
||
/// </summary>
|
||
public bool IsAuthenticated
|
||
{
|
||
get { return _IsAuthenticated; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取用于存储 Forms 身份验证票证的 Cookie 名称。默认值是“.ASPXAUTH”。
|
||
/// </summary>
|
||
public string FormsCookieName
|
||
{
|
||
get { return _FormsCookieName; }
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 构造函数
|
||
/// <summary>
|
||
/// 创建一个 Authentication 对象
|
||
/// </summary>
|
||
public Authentication()
|
||
{
|
||
_IsAuthenticated = System.Web.HttpContext.Current.Request.IsAuthenticated;
|
||
}
|
||
#endregion
|
||
|
||
#region 公共方法
|
||
|
||
/// <summary>
|
||
/// 将验证过的票据 写在 HttpContext.User 中
|
||
/// </summary>
|
||
/// <param name="sender">Global全局对象</param>
|
||
public void RequestAuthenticate(object sender)
|
||
{
|
||
try
|
||
{
|
||
HttpApplication app = sender as HttpApplication;
|
||
HttpContext context = app.Context;
|
||
if (_IsAuthenticated)
|
||
{
|
||
FormsIdentity identity = context.User.Identity as FormsIdentity;
|
||
FormsAuthenticationTicket ticket = identity.Ticket;
|
||
String[] roles = ticket.UserData.Split(',');
|
||
context.User = new System.Security.Principal.GenericPrincipal(identity, roles);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 使用 Cookie 名、版本、过期日期、发布日期、持久性以及用户特定的数据初始化 System.Web.Security.FormsAuthenticationTicket
|
||
/// 类的新实例。Cookie 路径设置为在应用程序的配置文件中建立的默认值。
|
||
/// </summary>
|
||
/// <param name="version">票证的版本号</param>
|
||
/// <param name="name">与票证关联的用户名</param>
|
||
/// <param name="issueDate">票证发出时的本地日期和时间</param>
|
||
/// <param name="expiration">票证过期时的本地日期和时间</param>
|
||
/// <param name="isPersistent">如果票证将存储在持久性 Cookie 中(跨浏览器会话保存),则为 true;否则为 false。如果该票证存储在 URL 中,将忽略此值。Cookie</param>
|
||
/// <param name="userData">存储在票证中的用户特定的数据。</param>
|
||
public void CreateAuthenticate(int version, string name, DateTime issueDate, DateTime expiration, bool isPersistent, string userData)
|
||
{
|
||
try
|
||
{
|
||
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(
|
||
version,
|
||
name,
|
||
issueDate,
|
||
expiration,
|
||
isPersistent,
|
||
userData);
|
||
String HachTicket = FormsAuthentication.Encrypt(Ticket);
|
||
HttpCookie httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HachTicket);
|
||
_FormsCookieName = FormsAuthentication.FormsCookieName;
|
||
System.Web.HttpContext.Current.Response.Cookies.Add(httpCookie);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 使用 cookie 名、版本、目录路径、发布日期、过期日期、持久性以及用户定义的数据初始化 System.Web.Security.FormsAuthenticationTicket
|
||
// 类的新实例。
|
||
/// </summary>
|
||
/// <param name="version">票证的版本号</param>
|
||
/// <param name="name">与票证关联的用户名</param>
|
||
/// <param name="issueDate">票证发出时的本地日期和时间</param>
|
||
/// <param name="expiration">票证过期时的本地日期和时间</param>
|
||
/// <param name="isPersistent">如果票证将存储在持久性 Cookie 中(跨浏览器会话保存),则为 true;否则为 false。如果该票证存储在 URL 中,将忽略此值。Cookie</param>
|
||
/// <param name="userData">存储在票证中的用户特定的数据。</param>
|
||
/// <param name="cookiePath">票证存储在 Cookie 中时的路径。</param>
|
||
public void CreateAuthenticate(int version, string name, DateTime issueDate, DateTime expiration, bool isPersistent, string userData, string cookiePath)
|
||
{
|
||
try
|
||
{
|
||
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(
|
||
version,
|
||
name,
|
||
issueDate,
|
||
expiration,
|
||
isPersistent,
|
||
userData,
|
||
cookiePath);
|
||
String HachTicket = FormsAuthentication.Encrypt(Ticket);
|
||
HttpCookie httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HachTicket);
|
||
_FormsCookieName = FormsAuthentication.FormsCookieName;
|
||
System.Web.HttpContext.Current.Response.Cookies.Add(httpCookie);
|
||
}
|
||
catch(Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从浏览器中删除 Forms 身份验证
|
||
/// </summary>
|
||
public void SignOut()
|
||
{
|
||
FormsAuthentication.SignOut();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 跳转到 ReturnUrl 页
|
||
/// </summary>
|
||
public void GoToUrl()
|
||
{
|
||
System.Web.HttpContext.Current.Response.Redirect("/_admin/Index.aspx");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 跳转到指定页
|
||
/// </summary>
|
||
public void GoToUrl(string url)
|
||
{
|
||
System.Web.HttpContext.Current.Response.Redirect(url);
|
||
}
|
||
#endregion
|
||
}
|
||
}
|