public class CustomAuthorizeAttribute : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { if (!filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) && !filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)) { var referrer = filterContext.HttpContext.Request.UrlReferrer == null ? null : filterContext.HttpContext.Request.UrlReferrer.Host; var ddd = filterContext.HttpContext.User.Identity.IsAuthenticated; if (!filterContext.HttpContext.Request.IsAuthenticated) { filterContext.Result = new RedirectResult("/Test/_login"); } } } }
如上,我在Mvc写了一个过滤器,验证
filterContext.HttpContext.Request.IsAuthenticated是否为true
并且我在Controller中写了如下代码让身份验证通过
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, "Uid", DateTime.Now, DateTime.Now.AddDays(1), true, "UserData"); HttpCookie Cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(Ticket));//加密身份信息,保存至Cookie Cookie.Expires = DateTime.Now.AddHours(12); Response.Cookies.Add(Cookie);
但是执行这些代码之后,过滤器的那个
IsAuthenticated一直为false,头大。
求助,困扰好久了。。。
Controller层的代码
private IUserService UserService; public AccountController(IUserService userService) { this.UserService = userService; base.AddDisposableObject(this.UserService); } [AllowAnonymous] public ActionResult Login(string returnUrl) { ViewBag.ReturnUrl = returnUrl; return View(); } //登录 [AllowAnonymous] [HttpPost] public ActionResult Login(string uid, string pwd, string returnUrl) { var mode = UserService.GetUserInfo(uid, pwd); if (mode != null) { SessionAccount userinfo = new SessionAccount {UserId = mode.uid, UserName = mode.uname}; SaveCookie(userinfo); } else { return Content("{\"user\":\"null\"}"); } if (!string.IsNullOrEmpty(returnUrl)) return RedirectToLocal(returnUrl); return RedirectToLocal("/account/index"); } private void SaveCookie(SessionAccount sessionAccount) { //HttpCookie authCookie = FormsAuthentication.GetAuthCookie(sessionAccount.UserId.ToString(), true); FormsAuthentication.SetAuthCookie(sessionAccount.UserId.ToString(),true); string UserData = SerializeHelper.JsonSerialize(sessionAccount);//序列化用户实体 //保存身份信息,参数说明可以看提示 FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, sessionAccount.UserId.ToString(), DateTime.Now, DateTime.Now.AddDays(1), true, UserData); HttpCookie Cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(Ticket));//加密身份信息,保存至Cookie Cookie.Expires = DateTime.Now.AddHours(12); Response.Cookies.Add(Cookie); } public ActionResult Index() { return View(); }
web.config是否设置了
<authentication mode="Forms">
登陆通过后设置
FormsAuthentication.SetAuthCookie(....)
<authentication mode="Forms"> <forms loginUrl="~/Account/Login" timeout="2880" /> </authentication>
FormsAuthentication.SetAuthCookie(sessionAccount.UserId.ToString(),true);
你这里写的有问题啊。你用了Authorize,在登录的时候为什么没有设置身份验证票证啊。
有的呀,忘了贴出来
FormsAuthentication.SetAuthCookie(sessionAccount.UserId.ToString(),true);
@Tzn: 你把
string UserData = SerializeHelper.JsonSerialize(sessionAccount);//序列化用户实体 //保存身份信息,参数说明可以看提示 FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, sessionAccount.UserId.ToString(), DateTime.Now, DateTime.Now.AddDays(1), true, UserData); HttpCookie Cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(Ticket));//加密身份信息,保存至Cookie Cookie.Expires = DateTime.Now.AddHours(12); Response.Cookies.Add(Cookie);
这注释掉试试
你先看下请求中是否携带了 cookie 嘛!