ASP.NET简单的内置权限管理(票据)认证,不用生成表即可判断是否有权限管理

1、 在根目录建立一个Global.asax文件,烤入一段代码

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
protected void Application_AuthenticateRequest( object SENDER, EventArgs e)
{
if (HttpContext.Current.User != null )
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id
= (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket tiecket
= id.Ticket;
string userData = tiecket.UserData;
string [] roles = userData.Split( ' , ' );
HttpContext.Current.User
= new System.Security.Principal.GenericPrincipal(id, roles);
}
}
}
}

2:  在web.config 文件中配置目录权限及登录页,

登录页,在system.web节点中

 
  
< authentication mode ="Forms" >
< forms name ="mycook" loginUrl ="login.aspx" protection ="All" path ="/" />
</ authentication >

配置目录权限,在system.web节点外面

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
< location path ="admin" >
< system .web >
< authorization >
< allow roles ="admin" />
< deny users ="*" />
</ authorization >
</ system.web >
</ location >
< location path ="user" >
< system .web >
< authorization >
< allow roles ="user" />
< deny users ="*" />
</ authorization >
</ system.web >
</ location >
< location path ="admin/admin_login.aspx" >
< system .web >
< authorization >
< allow users ="*" />
</ authorization >
</ system.web >
</ location >
< location path ="user/user_login.aspx" >
< system .web >
< authorization >
< allow users ="*" />
</ authorization >
</ system.web >
</ location >

 在登录页的登录事件中的登录成功后烤入一段代码

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
// string roles = "admin"; 代表用户角色 新添加
string roles = " admin " ;
HttpCookie cook;
string strReturnURL;
FormsAuthenticationTicket ticket
= new FormsAuthenticationTicket(
1 , user, DateTime.Now, DateTime.Now.AddMinutes( 30 ), false , roles);
cook
= new HttpCookie( " mycook " );
cook.Value
= FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(cook);
strReturnURL
= Request.Params[ " ReturnUrl " ];
if (strReturnURL != null && strReturnURL.Contains(".aspx") )
{
Response.Redirect(strReturnURL);
}
else
{
Session[
" 已经登录 " ] = true ;
Response.Redirect(
" index.aspx " );
}

 后台页面调用登录的用户名实例:

 
  
litname.Text = User.Identity.Name.ToString();

这样基本上就可以了

但是有个疑问 如果是多用户系统,用户没有登录就跳转到用户的登录页怎么办呢?

刚上面的办法是没办法跳转到2个登录页面的 这时候我们就需要建立一个中间的跳转登录页来根据ReturnURL中是否包含

admin 或者user来判断跳转到哪个登录页面了

建立 login_redirect.aspx

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace xh.shop.web
{
public partial class login_redirect : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
string strReturnURL = Request.Params[ " ReturnUrl " ];
if (strReturnURL != null && strReturnURL.Contains( " admin " ))
//包含的字段
{
Response.Redirect(
" admin/login.aspx ?ReturnUrl=" + strReturnURL);
//如果包含admin则跳转到否则跳转到***
}
else
{ Response.Redirect(
" index.aspx ?ReturnUrl=" + strReturnURL); }

}
}
}

最后config里面的loginurl改成 login_redirect.aspx就可以了

 
  
< authentication mode ="Forms" >
< forms name ="mycook" loginUrl ="login.aspx" protection ="All" path ="/" />
</ authentication >

正文补充知识:

可以用登录控件直接显示登录状态 登录名等

 
  
< asp:LoginView ID ="LoginView1" runat ="server" >
< AnonymousTemplate >
没有登录显示的样式
</ AnonymousTemplate >
< LoggedInTemplate >
登录后显示的样式
< br />< br />< br />< br />
你好!
< asp:LoginName ID ="LoginName1" runat ="server" />
< asp:LoginStatus ID ="LoginStatus1" runat ="server" />
</ LoggedInTemplate >
</ asp:LoginView >
注销函数
 
  
// 首先引入using System.Web.Security;
protected void loginout( object sender, EventArgs e)
{

FormsAuthentication.SignOut();
// 注销当前登录用户
}

转载于:https://www.cnblogs.com/luqingsong/archive/2011/01/03/1924998.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值