62,074
社区成员
发帖
与我相关
我的任务
分享
<authentication mode="Forms">
<forms loginUrl="Admin/Login.aspx" name=".ASPXAUTH" timeout="30" path="/"></forms>
</authentication>
<configuration>
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
<location path="DbManager.aspx">
<system.web>
<authorization>
<allow roles="Admins"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="CreateCode.aspx">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
<location path="Login.aspx">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
if (AdminBll.ValidateLogin(myAdmin) == LoginResult.OK)
{
ClientScript.RegisterStartupScript(Page.GetType(), "", MessageBox.Alert("登陆成功"));
string userRoles = "Admins"; //为测试方便直接填写的角色字符串
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, _UserName, DateTime.Now, DateTime.Now.AddMinutes(30), false, userRoles, "/");
string HaskTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie UserCookies = new HttpCookie(FormsAuthentication.FormsCookieName, HaskTicket);
Response.Cookies.Add(UserCookies);
if (string.IsNullOrEmpty(Request["ReturnUrl"]))
{
Response.Redirect("index.aspx");
}
else
{
Response.Redirect(Request["ReturnUrl"]);
}
}
void Application_AuthorizeRequest(object sender, System.EventArgs e)
{
HttpApplication App = (HttpApplication)sender;
HttpContext Ctx = App.Context; //获取本次Http请求相关的HttpContext对象
if (Ctx.Request.IsAuthenticated == true) //验证过的用户才进行role的处理
{
FormsIdentity Id = (FormsIdentity)Ctx.User.Identity;
FormsAuthenticationTicket Ticket = Id.Ticket; //取得身份验证票
string[] Roles = Ticket.UserData.Split(','); //将身份验证票中的role数据转成字符串数组
Ctx.User = new System.Security.Principal.GenericPrincipal(Id, Roles); //将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息
}
}
FormsIdentity Id = (FormsIdentity)User.Identity;
FormsAuthenticationTicket Ticket = Id.Ticket; //取得身份验证票
<location path="DbManager.aspx">
<system.web>
<authorization>
<allow roles="Admins"/>
<deny users="*"/>
</authorization>
</system.web>
</location>