using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
namespace Mtxfw.Utility
{
/* 2011.2.27 by Telesa */
///
/// FromsAuthentication 身份验证类
///
public class Authentication
{
#region 私有成员
private bool _IsAuthenticated = false;
private string _FormsCookieName = string.Empty;
#endregion
#region 公共属性
///
/// 获取一个值 指示是否验证了请求
///
public bool IsAuthenticated
{
get { return _IsAuthenticated; }
}
///
/// 获取用于存储 Forms 身份验证票证的 Cookie 名称。默认值是“.ASPXAUTH”。
///
public string FormsCookieName
{
get { return _FormsCookieName; }
}
#endregion
#region 构造函数
///
/// 创建一个 Authentication 对象
///
public Authentication()
{
_IsAuthenticated = System.Web.HttpContext.Current.Request.IsAuthenticated;
}
#endregion
#region 公共方法
///
/// 将验证过的票据 写在 HttpContext.User 中
///
/// Global全局对象
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;
}
}
///
/// 使用 Cookie 名、版本、过期日期、发布日期、持久性以及用户特定的数据初始化 System.Web.Security.FormsAuthenticationTicket
/// 类的新实例。Cookie 路径设置为在应用程序的配置文件中建立的默认值。
///
/// 票证的版本号
/// 与票证关联的用户名
/// 票证发出时的本地日期和时间
/// 票证过期时的本地日期和时间
/// 如果票证将存储在持久性 Cookie 中(跨浏览器会话保存),则为 true;否则为 false。如果该票证存储在 URL 中,将忽略此值。Cookie
/// 存储在票证中的用户特定的数据。
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;
}
}
///
/// 使用 cookie 名、版本、目录路径、发布日期、过期日期、持久性以及用户定义的数据初始化 System.Web.Security.FormsAuthenticationTicket
// 类的新实例。
///
/// 票证的版本号
/// 与票证关联的用户名
/// 票证发出时的本地日期和时间
/// 票证过期时的本地日期和时间
/// 如果票证将存储在持久性 Cookie 中(跨浏览器会话保存),则为 true;否则为 false。如果该票证存储在 URL 中,将忽略此值。Cookie
/// 存储在票证中的用户特定的数据。
/// 票证存储在 Cookie 中时的路径。
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;
}
}
///
/// 从浏览器中删除 Forms 身份验证
///
public void SignOut()
{
FormsAuthentication.SignOut();
}
///
/// 跳转到 ReturnUrl 页
///
public void GoToUrl()
{
System.Web.HttpContext.Current.Response.Redirect("/_admin/Index.aspx");
}
///
/// 跳转到指定页
///
public void GoToUrl(string url)
{
System.Web.HttpContext.Current.Response.Redirect(url);
}
#endregion
}
}