登录时,使用 Forms验证,
string userData = UserProfileIO.Serialize(account);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
account.Name,
System.DateTime.Now,
System.DateTime.Now.AddDays(30),
false,
userData,
FormsAuthentication.FormsCookiePath);
string encTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie cokie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);//FormsAuthentication .FormsCookieName配置文件的cookie名称
cokie.Expires = DateTime.Now.AddDays(5);
Response.Cookies.Add(cokie);
但登录后,
Context.User.Identity as FormsIdentity 为空
Request.IsAuthenticated 为 false
配置
环境 VS2013 MVC5 .net4.5
web.comfig
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="~/Account" defaultUrl="~/" timeout="2880" />
</authentication>
Global.asax.cs :
void MvcApplication_AuthorizeRequest(object sender, EventArgs e)
{
var id = Context.User.Identity as FormsIdentity;
if (id != null && id.IsAuthenticated)
{
var roles = id.Ticket.UserData ;
}
}
求指教
mvc5 好像移除了forms验证模块了.可以看一下这个.
没有这句吧 FormsAuthentication.SetAuthCookie(user.UserName, RememberMe);