FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
"ticketName",
DateTime.Now,
DateTime.Now.AddMinutes(60),
false,
"{UserNAme:'sanmao'}"
);
string token = FormsAuthentication.Encrypt(ticket);
HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, token);
Response.Cookies.Add(userCookie);
string url = FormsAuthentication.GetRedirectUrl(UserName, false);//获取来源地址
Response.Redirect(url);
System.Web.HttpContext context = System.Web.HttpContext.Current;
// 取得用户对象
System.Security.Principal.IPrincipal user = context.User;
// 取得用户的角色数组
System.Web.Security.FormsIdentity fi = user.Identity as System.Web.Security.FormsIdentity;
// 取得用户的票据
System.Web.Security.FormsAuthenticationTicket ticket = fi.Ticket;
// 创建用户所拥有的角色数组
string roleString = ticket.UserData;
// 还原为字符串数组
string[] roleArray = { roleString };//roleString.Split(',');
// 自己创建用户对象
System.Security.Principal.GenericPrincipal principal
= new System.Security.Principal.GenericPrincipal(
user.Identity,
roleArray
);
// 让系统使用我们的拥有角色的用户对象
context.User = principal;
Response.Write(tt);