|
asp.net身份验证和授权(6) </form> </body> 后置代码
private void Page_Load(object sender, System.EventArgs e) { this.lbUser.Text = User.Identity.Name; if (User.IsInRole("Admin")) this.lbSf.Text = "Admin"; else this.lbSf.Text = "User"; }
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /**//// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.btnLogout.Click += new System.EventHandler(this.btnLogout_Click); this.Load += new System.EventHandler(this.Page_Load);
} #endregion
private void btnLogout_Click(object sender, System.EventArgs e) { FormsAuthentication.SignOut();//注销票 Response.Redirect("login.aspx",true);返回login.aspx页面 }
webconfig配置如下 <authentication mode="Forms" > <forms name=".SecurityDemo" loginUrl="login.aspx">//.SecurityDemo为cookie名, </forms> </authentication>
<authorization> <deny users="?"/> //拒绝所有匿名用户 <allow roles="admins"/>//允许管理级别用户访问 </authorization> 自我感觉ASP写多了,一般是用session进行判断用户是否合法,但在一个ASP.NET项目中使用身份验证,基本上所有页面都要验证才能访问,感觉有点迁强.但可以在web.config页面对指定的页面设置权限,设置代码如下
|