现在的位置: 首页 > 综合 > 正文

TheBeerHouse 系列四:从表示层开始下(我怎样限制你的自由??)

2012年03月28日 ⁄ 综合 ⁄ 共 6013字 ⁄ 字号 评论关闭
- -开始胡说八道前
先说说页面的结构~~上次忘了说

-就是这样的~~有意思点意思吧~~首先它几乎所有的页面页的 MasterPageFile="~/Template.master",但是后台都来自于自定义类: BasePage(上有政策~~下有对策阿)

我们先来看看,Template.master

public partial class TemplateMaster : System.Web.UI.MasterPage
   
{
      
private bool _enablePersonalization = false;
       
/// <summary>
      
/// 是否显示PersonalizationManager,他是用来管理WebPar
       
/// </summary>

      public bool EnablePersonalization
      
{
         
get return _enablePersonalization; }
         
set
         
{
            _enablePersonalization 
= value;
            
//--- --当用户登录并且输入的是true的时候才显示--也就是说~~不登陆要操作WebPar没门
            PersonalizationManager1.Visible = (this.Page.User.Identity.IsAuthenticated && value);
         }

      }
 protected void Page_Load(object sender, EventArgs e)
      
{

    
//--外国朋友的安全意识高啊 - -这里如果没有通过验证则不显示
         if (!this.Page.User.Identity.IsAuthenticated)
            PersonalizationManager1.Visible 
= false;
      }

}

----但我认为 EnablePersonalization是个双重保险,当我们输入false即便用户登录了也显示阿,关于
PersonalizationManager-可以看看系列系列三

第二 BasePage



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MB.TheBeerHouse.BLL.Store;

namespace MB.TheBeerHouse.UI
{
   
public class BasePage : System.Web.UI.Page
   
{
      
protected override void InitializeCulture()
      
{
         
string culture = (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture;
          
//--设置设置与页面向关联的区域性ID
         this.Culture = culture;
          
//--UiId
         this.UICulture = culture;
      }


      
protected override void OnPreInit(EventArgs e)
      
{
}


      
protected override void OnLoad(EventArgs e)
      
{
         
// add onfocus and onblur javascripts to all input controls on the forum,
         
// so that the active control has a difference appearance
          
//---给整个控件在函数中定义的类型挂样式表---------false代表第一次传送的是顶级控件不挂样式表,如果该控件还有子控件则递规调用
          
//SetInputControlsHighlight继续挂样式表
         Helpers.SetInputControlsHighlight(this"highlight"false);

         
base.OnLoad(e);
      }


       
/// <summary>
      
/// 获得asp.net比如这里是/TBH_Web 
       
/// </summary>

     public string BaseUrl
    
{
        
get
        
{
            
//--判断下结尾又没有/
            string url = this.Request.ApplicationPath;
            
if (url.EndsWith("/"))
                
return url;
            
else
                
return url + "/";
        }

    }


    
//--FullBaseUrl = "http://localhost:/TBH_Web/",--就是得到网络路径的根目录
    public string FullBaseUrl
    
{
        
get
        
{//--this.Request.Url.AbsoluteUri = "http://localhost/TBH_Web/Default3.aspx"
            return this.Request.Url.AbsoluteUri.Replace(
               
this.Request.Url.PathAndQuery, ""+ this.BaseUrl;
            
//-this.Request.Url.PathAndQuery = "/TBH_Web/Default3.aspx"
        }

    }


       
//---跳转的登陆页
      protected void RequestLogin()
      
{
         
this.Response.Redirect(FormsAuthentication.LoginUrl + 
            
"?ReturnUrl=" + this.Request.Url.PathAndQuery);
      }


      
public string FormatPrice(object price)
      
{
         
return Convert.ToDecimal(price).ToString("N2"+ " " + Globals.Settings.Store.CurrencyCode;
      }

   }

}

//--

- -ok基本上就是这些了让我来进入正题吧

上一篇~~曾经讲过一种权限管理~~不过是自动的这次让我们来看看- -手动怎么管理阿- -

namespace MB.TheBeerHouse.UI.Admin
{
   
public partial class _Default : BasePage
   
{
      
protected void Page_Load(object sender, EventArgs e)
      
{
    
//--权限审核
         panAdmin.Visible = (this.User.IsInRole("Administrators"));
         panEditor.Visible 
= (this.User.IsInRole("Administrators"|| this.User.IsInRole("Editors"));
         panStoreKeeper.Visible 
= (this.User.IsInRole("Administrators"|| this.User.IsInRole("StoreKeepers"));
         panModerator.Visible 
= (this.User.IsInRole("Administrators"|| this.User.IsInRole("Editors"|| this.User.IsInRole("Modearators"));
         panContributor.Visible 
= (this.User.IsInRole("Administrators"|| this.User.IsInRole("Editors"|| this.User.IsInRole("Contributors"));
      }

   }

}

- -接下来看看如何配置这些权限

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MB.TheBeerHouse;

namespace MB.TheBeerHouse.UI.Admin
{
   
public partial class EditUser : BasePage
   
{
      
string userName = "";

      
protected void Page_Load(object sender, EventArgs e)
      
{
         
// retrieve the username from the querystring
         userName = this.Request.QueryString["UserName"];

         lblRolesFeedbackOK.Visible 
= false;
         lblProfileFeedbackOK.Visible 
= false;

         
if (!this.IsPostBack)
         
{
             
//--这个是编辑Profile的自定义控件
            UserProfile1.UserName = userName;

            
// 取得用户信息
            MembershipUser user = Membership.GetUser(userName);
            lblUserName.Text 
= user.UserName;
            lnkEmail.Text 
= user.Email;
            lnkEmail.NavigateUrl 
= "mailto:" + user.Email;
             
//--得到注册时间
            lblRegistered.Text = user.CreationDate.ToString("f");
             
//--得到上次进行身份验证的时间
            lblLastLogin.Text = user.LastLoginDate.ToString("f");
            lblLastActivity.Text 
= user.LastActivityDate.ToString("f");
             
//--是否在线
            chkOnlineNow.Checked = user.IsOnline;

             
//--是否可以用成员资格验证
            chkApproved.Checked = user.IsApproved;

             
//--这里比较有意思--首先--反映是否因为被锁定而不能验证
            chkLockedOut.Checked = user.IsLockedOut;

            
//---如果没有锁定那么不显示上面的chkLockedOut控件
            chkLockedOut.Enabled = user.IsLockedOut;

            BindRoles();
         }

      }

       
/// <summary>
       
/// 绑定角色信息
       
/// </summary>

      private void BindRoles()
      
{
         
// fill the CheckBoxList with all the available roles, and then select
         
// those that the user belongs to

          
//---得到所有角色
         chklRoles.DataSource = Roles.GetAllRoles();
         chklRoles.DataBind();
 

抱歉!评论已关闭.