您的位置:首页 > 编程语言 > ASP

asp.net form 验证 和取值

2012-05-18 16:53 232 查看
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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: