配置文件:
<system.web> <authentication mode="Forms"> <!-- 如果验证失败就返回URL的指定界面,设置cookie的时间和名字--> <forms name="Roles" loginUrl="~/Home/pp" timeout="2880" cookieless="UseCookies"></forms> </authentication> </system.web>
后台:
// // GET: /Home/ public ActionResult Index() { return View(); } [AllowAnonymous]//默认情况,可以不用验证身份 public ActionResult pp() { //HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, "B"); //System.Web.HttpContext.Current.Response.Cookies.Add(cookie); //设置cookie的值 FormsAuthentication.SetAuthCookie("b", false); return View(); } //[AllowAnonymous]//默认情况,可以不用验证身份 public ActionResult CheckData() { return View("Index"); }