[转]在Asp.Net的论坛中实现角色机制

构造类:

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值