在根目录建立一个Globalasax文件烤入一段代码
代码
protected void Application_AuthenticateRequest(object SENDER EventArgs e)
{
if (HttpContextCurrentUser != null)
{
if (HttpContextCurrentUserIdentityIsAuthenticated)
{
if (HttpContextCurrentUserIdentity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContextCurrentUserIdentity;
FormsAuthenticationTicket tiecket = idTicket;
string userData = tiecketUserData;
string[] roles = userDataSplit();
HttpContextCurrentUser = new SystemSecurityPrincipalGenericPrincipal(id roles);
}
}
}
}
: 在nfig 文件中配置目录权限及登录页
登录页在systemweb节点中
<authentication mode=Forms>
<forms name=mycook loginUrl=loginaspx protection=All path=//>
</authentication>
配置目录权限在systemweb节点外面
代码
<location path=admin>
<systemweb>
<authorization>
<allow roles=admin/>
<deny users=*/>
</authorization>
</systemweb>
</location>
<location path=user>
<systemweb>
<authorization>
<allow roles=user/>
<deny users=*/>
</authorization>
</systemweb>
</location>
<location path=admin/admin_loginaspx>
<systemweb>
<authorization>
<allow users=*/>
</authorization>
</systemweb>
</location>
<location path=user/user_loginaspx>
<systemweb>
<authorization>
<allow users=*/>
</authorization>
</systemweb>
</location>
代码
HttpCookie cook;
string strReturnURL;
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
name DateTimeNow DateTimeNowAddMinutes() false lv);
cook = new HttpCookie(mycook);
cookValue = FormsAuthenticationEncrypt(ticket);
ResponseCookiesAdd(cook);
strReturnURL = RequestParams[ReturnUrl];
if (strReturnURL != null)
{
ResponseRedirect(strReturnURL);
}
else
{
ResponseRedirect(Defaultaspx);
}