以前写死的写法是
1: //设置登录权限
2: HttpCookie cook;
3:
4: string roles = "admin";//用户角色
5:
6: FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
7:
8: 1, tid, DateTime.Now, DateTime.Now.AddMinutes(600), true, roles);
9:
10: cook = new HttpCookie(".xxxxCookie");
11: cook.Domain = ".youxxxxx.com";
12: cook.Value = FormsAuthentication.Encrypt(ticket);
13:
14: Response.Cookies.Add(cook);
15: //权限结束
16:
动态读取的写法是
1: //设置登录权限
2: HttpCookie cook;
3:
4: string roles = "admin";//用户角色
5:
6: FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
7:
8: 1, tid, DateTime.Now, DateTime.Now.AddMinutes(600), true, roles);
9:
10: AuthenticationSection authen = WebConfigurationManager.GetSection("system.web/authentication") as AuthenticationSection;
11: string cookName = authen.Forms.Name;
12: string cookDomain = authen.Forms.Domain;
13: cook = new HttpCookie(cookName); //".xxxxCookie"
14: cook.Domain = cookDomain; //.youxxxx.com
15: cook.Value = FormsAuthentication.Encrypt(ticket);
16:
17: Response.Cookies.Add(cook);
18: //权限结束
19:
动态读取的好处就是,你修改了web.config里面的配置,我就不用去登陆界面修改cookies的名称和域名等信息了
本文详细介绍了动态读取Cookies的方法及其在ASP.NET中的应用,阐述了其相较于静态设置的优势,主要涉及Forms Authentication机制及如何通过Web.config配置文件动态管理Cookies。

327

被折叠的 条评论
为什么被折叠?



