构造类:
using System;
using System.Collections;
using System.Security.Principal;
namespace Wuyin.Forums.Components
{
internal sealed class ForumsPrincipal : IPrincipal
{
private IIdentity _identity;
private string [] _roles;
public ForumsPrincipal(IIdentity identity, string [] roles)
{
_identity = identity;
_roles = new string[roles.Length];
roles.CopyTo(_roles, 0);
Array.Sort(_roles);
}
// IPrincipal Implementation
public bool IsInRole( string role)
{
return Array.BinarySearch( _roles, role ) > 0 ? true : false;
}
public IIdentity Identity
{
get
{
return _identity;
}
}
// Checks whether a principal is in all of the specified set of roles
public bool IsInAllRoles( params string [] roles )
{
foreach ( string searchrole in roles )
{
if (Array.BinarySearch(_roles, searchrole) < 0 )
return false;
}
return true;
}
// Checks whether a principal is in any of the specified set of roles
public bool IsInAnyRoles( params string [] roles )
{
foreach ( string searchrole in roles )
{
if (Array.BinarySearch(_roles, searchrole ) > 0 )
return true;
}
return false;
}
}
}
UserRole类:
using System;
using System.Web;
using System.Web.Security;
namespace Wuyin.Forums
{
/// <summary>
/// UserRoles 的摘要说明。
/// </summary>
public class UserRoles
{
public UserRoles()
{
//
// TODO: 在此处添加构造函数逻辑
//
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = System.Web.HttpContext.Current.Request.Cookies[cookieName];
if( null == authCookie)
{
// There is no authentication cookie.
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch
{
// Log exception details (omitted for simplicity)
return;
}
if ( null == authTicket)
{
// Cookie failed to decrypt.
return;
}
// When the ticket was created, the UserData property was assigned a
// pipe delimited string of role names.
string[] roles = authTicket.UserData.Split('$');
// Create an Identity object
FormsIdentity id = new FormsIdentity( authTicket );
// This principal will flow throughout the request.
Wuyin.Forums.Components.ForumsPrincipal principal = new Wuyin.Forums.Components.ForumsPrincipal(id, roles);
// Attach the new principal object to the current HttpContext object
System.Web.HttpContext.Current.User = principal;
}
public static void AddForumToRole( int forumID, string role)
{
}
public static void AddUserToRole( string username, string role)
{
}
public static void CreateNewRole( string role, string description)
{
}
public static void DeleteRole( string role)
{
}
public static string[] GetAllRoles()
{
//Wuyin.Forums.Data.SqlDataProvider Wuyin.Forums.Data.DataProvider.Instance().= new Wuyin.Forums.Data.SqlDataProvider();
string[] s = Wuyin.Forums.Data.DataProvider.Instance().GetAllRoles();
// Wuyin.Forums.Data.DataProvider.Instance().Dispose();
return s;
}
public static string[] GetForumRoles( int forumID)
{
//Wuyin.Forums.Data.SqlDataProvider Wuyin.Forums.Data.DataProvider.Instance().= new Wuyin.Forums.Data.SqlDataProvider();
string[] s = Wuyin.Forums.Data.DataProvider.Instance().GetForumRoles(forumID);
//Wuyin.Forums.Data.DataProvider.Instance().Dispose();
return s;
}
public static string GetRoleDescription( string role)
{
//Wuyin.Forums.Data.SqlDataProvider Wuyin.Forums.Data.DataProvider.Instance().= new Wuyin.Forums.Data.SqlDataProvider();
string s = Wuyin.Forums.Data.DataProvider.Instance().GetRoleDescription(role).Get("Description").ToString();
// Wuyin.Forums.Data.DataProvider.Instance().Dispose();
return s;
}
public void GetUserRoles()
{
string roles="";
string[] userRoles = UserRoles.GetAllRoles();
for( int i=0;i<userRoles.Length;i++)
{
roles+=userRoles[i];
if(i<userRoles.Length)
roles+="$";
}
//this.Controls.Add(new System.Web.UI.LiteralControl(roles));
//return;
// Create the authentication ticket
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, null,DateTime.Now,DateTime.MaxValue, true,roles); // User data
// Now encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
// Create a cookie and add the encrypted ticket to the
// cookie as data.
HttpCookie rolesCookie = new HttpCookie("WuyinForumsRoles",encryptedTicket);
System.Web.HttpContext.Current.Response.Cookies.Add(rolesCookie);
}
public static string[] GetUserRoles( string username)
{
//Wuyin.Forums.Data.SqlDataProvider Wuyin.Forums.Data.DataProvider.Instance().= new Wuyin.Forums.Data.SqlDataProvider();
string[] s = Wuyin.Forums.Data.DataProvider.Instance().GetUserRoles(username);
//Wuyin.Forums.Data.DataProvider.Instance().Dispose();
return s;
}
public static void RemoveForumFromRole( int forumID, string role)
{
}
public static void RemoveUserFromRole( string username, string role)
{
}
public static void SignOut()
{
FormsAuthentication.SignOut();
}
public static void UpdateRole( string role, string description)
{
}
}
}
在Global.asax中:
<script language="C#" runat="server">
void Application_AuthenticateRequest(Object sender, EventArgs e)
{
UserRoles forumRoles = new UserRoles();
forumRoles.GetUserRoles();
}
</script>
using System;
using System.Collections;
using System.Security.Principal;
namespace Wuyin.Forums.Components
{
internal sealed class ForumsPrincipal : IPrincipal
{
private IIdentity _identity;
private string [] _roles;
public ForumsPrincipal(IIdentity identity, string [] roles)
{
_identity = identity;
_roles = new string[roles.Length];
roles.CopyTo(_roles, 0);
Array.Sort(_roles);
}
// IPrincipal Implementation
public bool IsInRole( string role)
{
return Array.BinarySearch( _roles, role ) > 0 ? true : false;
}
public IIdentity Identity
{
get
{
return _identity;
}
}
// Checks whether a principal is in all of the specified set of roles
public bool IsInAllRoles( params string [] roles )
{
foreach ( string searchrole in roles )
{
if (Array.BinarySearch(_roles, searchrole) < 0 )
return false;
}
return true;
}
// Checks whether a principal is in any of the specified set of roles
public bool IsInAnyRoles( params string [] roles )
{
foreach ( string searchrole in roles )
{
if (Array.BinarySearch(_roles, searchrole ) > 0 )
return true;
}
return false;
}
}
}
UserRole类:
using System;
using System.Web;
using System.Web.Security;
namespace Wuyin.Forums
{
/// <summary>
/// UserRoles 的摘要说明。
/// </summary>
public class UserRoles
{
public UserRoles()
{
//
// TODO: 在此处添加构造函数逻辑
//
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = System.Web.HttpContext.Current.Request.Cookies[cookieName];
if( null == authCookie)
{
// There is no authentication cookie.
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch
{
// Log exception details (omitted for simplicity)
return;
}
if ( null == authTicket)
{
// Cookie failed to decrypt.
return;
}
// When the ticket was created, the UserData property was assigned a
// pipe delimited string of role names.
string[] roles = authTicket.UserData.Split('$');
// Create an Identity object
FormsIdentity id = new FormsIdentity( authTicket );
// This principal will flow throughout the request.
Wuyin.Forums.Components.ForumsPrincipal principal = new Wuyin.Forums.Components.ForumsPrincipal(id, roles);
// Attach the new principal object to the current HttpContext object
System.Web.HttpContext.Current.User = principal;
}
public static void AddForumToRole( int forumID, string role)
{
}
public static void AddUserToRole( string username, string role)
{
}
public static void CreateNewRole( string role, string description)
{
}
public static void DeleteRole( string role)
{
}
public static string[] GetAllRoles()
{
//Wuyin.Forums.Data.SqlDataProvider Wuyin.Forums.Data.DataProvider.Instance().= new Wuyin.Forums.Data.SqlDataProvider();
string[] s = Wuyin.Forums.Data.DataProvider.Instance().GetAllRoles();
// Wuyin.Forums.Data.DataProvider.Instance().Dispose();
return s;
}
public static string[] GetForumRoles( int forumID)
{
//Wuyin.Forums.Data.SqlDataProvider Wuyin.Forums.Data.DataProvider.Instance().= new Wuyin.Forums.Data.SqlDataProvider();
string[] s = Wuyin.Forums.Data.DataProvider.Instance().GetForumRoles(forumID);
//Wuyin.Forums.Data.DataProvider.Instance().Dispose();
return s;
}
public static string GetRoleDescription( string role)
{
//Wuyin.Forums.Data.SqlDataProvider Wuyin.Forums.Data.DataProvider.Instance().= new Wuyin.Forums.Data.SqlDataProvider();
string s = Wuyin.Forums.Data.DataProvider.Instance().GetRoleDescription(role).Get("Description").ToString();
// Wuyin.Forums.Data.DataProvider.Instance().Dispose();
return s;
}
public void GetUserRoles()
{
string roles="";
string[] userRoles = UserRoles.GetAllRoles();
for( int i=0;i<userRoles.Length;i++)
{
roles+=userRoles[i];
if(i<userRoles.Length)
roles+="$";
}
//this.Controls.Add(new System.Web.UI.LiteralControl(roles));
//return;
// Create the authentication ticket
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, null,DateTime.Now,DateTime.MaxValue, true,roles); // User data
// Now encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
// Create a cookie and add the encrypted ticket to the
// cookie as data.
HttpCookie rolesCookie = new HttpCookie("WuyinForumsRoles",encryptedTicket);
System.Web.HttpContext.Current.Response.Cookies.Add(rolesCookie);
}
public static string[] GetUserRoles( string username)
{
//Wuyin.Forums.Data.SqlDataProvider Wuyin.Forums.Data.DataProvider.Instance().= new Wuyin.Forums.Data.SqlDataProvider();
string[] s = Wuyin.Forums.Data.DataProvider.Instance().GetUserRoles(username);
//Wuyin.Forums.Data.DataProvider.Instance().Dispose();
return s;
}
public static void RemoveForumFromRole( int forumID, string role)
{
}
public static void RemoveUserFromRole( string username, string role)
{
}
public static void SignOut()
{
FormsAuthentication.SignOut();
}
public static void UpdateRole( string role, string description)
{
}
}
}
在Global.asax中:
<script language="C#" runat="server">
void Application_AuthenticateRequest(Object sender, EventArgs e)
{
UserRoles forumRoles = new UserRoles();
forumRoles.GetUserRoles();
}
</script>