Forms表单登陆,动态获取web.config里面的cookies配置

image

以前写死的写法是

 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)