<?xml version="1.0"?>
<configuration>
<system.web>
<!--
通过 <authentication> 节可以配置 ASP.NET 用来
识别进入用户的
安全身份验证模式。
-->
<authentication mode="Forms">
<forms name=".AYSDLJTAQXH" loginUrl="login.aspx" >
<credentials passwordFormat="SHA1"/>
</forms>
</authentication>
</system.web>
<!--锁定指定目录-->
<location path="Administer">
<system.web>
<authorization>
<allow roles="administers,admins"/>
<deny users="?"/>
</authorization>
</system.web>
</location>
<location path="Member">
<system.web>
<authorization>
<allow roles="administers,members"/>
<deny users="?"/>
</authorization>
</system.web>
</location>
</configuration>
在web.config中,我使用Forms验证模式。
<location path="Administer">和<location path="Member">分别是要锁定的目录。
<allow roles="administers,admins"/>和<allow roles="administers,members"/>是允许哪些角色名称有限访问。看到web.config中的信息,相信大家已经了解我的用意。我想admins用户只能访问Administer目录,members用户只能访问Member目录,而administers能访问所有目录。我有一个Cookie.cs文件用来保存Cookie文件。在Cookie文件中使用FormsAuthenticationTicket建立身份验证票对象我现在的问题是,不管什么用户只要登录成功就能访问任何目录。<allow roles="administers,admins"/>和<allow roles="administers,members"/>好像没有起到作用。后来发现FormsAuthenticationTicket好像没有建立角色名称的功能?请问怎么才能让用户各访问各的目录呢?
<configuration>
<system.web>
<!--
通过 <authentication> 节可以配置 ASP.NET 用来
识别进入用户的
安全身份验证模式。
-->
<authentication mode="Forms">
<forms name=".AYSDLJTAQXH" loginUrl="login.aspx" >
<credentials passwordFormat="SHA1"/>
</forms>
</authentication>
</system.web>
<!--锁定指定目录-->
<location path="Administer">
<system.web>
<authorization>
<allow roles="administers,admins"/>
<deny users="?"/>
</authorization>
</system.web>
</location>
<location path="Member">
<system.web>
<authorization>
<allow roles="administers,members"/>
<deny users="?"/>
</authorization>
</system.web>
</location>
</configuration>
在web.config中,我使用Forms验证模式。
<location path="Administer">和<location path="Member">分别是要锁定的目录。
<allow roles="administers,admins"/>和<allow roles="administers,members"/>是允许哪些角色名称有限访问。看到web.config中的信息,相信大家已经了解我的用意。我想admins用户只能访问Administer目录,members用户只能访问Member目录,而administers能访问所有目录。我有一个Cookie.cs文件用来保存Cookie文件。在Cookie文件中使用FormsAuthenticationTicket建立身份验证票对象我现在的问题是,不管什么用户只要登录成功就能访问任何目录。<allow roles="administers,admins"/>和<allow roles="administers,members"/>好像没有起到作用。后来发现FormsAuthenticationTicket好像没有建立角色名称的功能?请问怎么才能让用户各访问各的目录呢?
解决方案 »
- VS2008能否内嵌SQL2008 Express版到集成环境中
- aspnetpager在不跟数据控件结合的时候,该如何分页?
- flash游戏网站,如何防止swf文件盗链,谢谢
- 求职(asp.net/c#)
- aspx 页面状态
- 有人熟悉aspx网页不?找人帮个忙。
- DropDownList绑定数据的问题。
- 有没有对用户控件里面的用户控件(datagrid) ,动态添加模板列,模板列里面加入dropdownlist, 再外露它的selectedIndexChange事件, 等作一个
- 所有分都拿出来了.如何在DataList的itemtemplate模板中插入一个DataGrid,在线等,
- 查询数据库时出了问题
- 不能调度ASP.NET程序,也不能启动Terminal Services服务
- CheckBox的问题
FormsAuthenticationTicket ticket=new FormsAuthenticationTicket (1,loginId,DateTime.Now,DateTime.Now.AddMinutes(30),false,userRole,"/");
先感谢一下 imfor 的回复,他说的不错,这是第一点。
再感谢 hanyise520 的回复,虽然我知道,但是要给兄弟们说一下,这也很重要,这是第二点。
第三点就是在网站下面创建一个Global.asax文件来识别票证中的用户角色。在其中添加如下方法。
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)
{
string userData;
string[] roles; userData = string.Empty;
try
{
if (Request.Cookies[".AYSDLJTAQXH"] != null)
{
FormsAuthenticationTicket ticket =
FormsAuthentication.Decrypt(Request.Cookies[".AYSDLJTAQXH"].Value);
if (ticket != null)
{
userData = ticket.UserData;
}
}
}
catch (Exception E)
{
HttpContext.Current.Response.Write("<!-- " + E.Message + " -->");
}
roles = userData.Split(',');
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(HttpContext.Current.User.Identity, roles);
}
}
}
}