<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="login.aspx"></forms>
</authentication>
<!--<authorization>
<allow users="*"></allow>
</authorization>-->
</system.web>
</configuration>
<?xml version="1.0"?> <configuration> <system.web> <authorization> <allow roles="admin" /> <deny users="*"/> </authorization> </system.web> </configuration>
<?xml version="1.0"?> <configuration> <system.web> <authorization> <allow roles="teacher" /> <deny users="*"/> </authorization> </system.web> </configuration>
<?xml version="1.0"?> <configuration> <system.web> <authorization> <allow roles="student" /> <deny users="*"/> </authorization> </system.web> </configuration>
protected void SetLoginCookie(string username, string roles)
{
System.Web.Security.FormsAuthentication.SetAuthCookie(username, false);
System.Web.Security.FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddDays(1), false, roles, "/");
string hashTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
HttpContext.Current.Response.SetCookie(userCookie);
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext ctx = app.Context; //获取本次Http请求的HttpContext对象
if (ctx.User != null)
{
if (ctx.Request.IsAuthenticated == true) //验证过的一般用户才能进行角色验证
{
System.Web.Security.FormsIdentity fi = (System.Web.Security.FormsIdentity)ctx.User.Identity;
System.Web.Security.FormsAuthenticationTicket ticket = fi.Ticket; //取得身份验证票
string userData = ticket.UserData;//从UserData中恢复role信息
string[] roles = userData.Split(','); //将角色数据转成字符串数组,得到相关的角色信息
ctx.User = new System.Security.Principal.GenericPrincipal(fi, roles); //这样当前用户就拥有角色信息了
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有