我现在想学ASP.NET,我看别人的代码,发现下面一句
FormsAuthentication.RedirectFromLoginPage(userId, false);
能重定向至另一页面,但这里并没有指定要转向那个页面啊,
原来在web.config有配置啊
[code]<authentication mode="Forms" >
<forms loginUrl="login.aspx"></forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization> [/code]
呵呵,这个东西很爽的,当用了FORM认证,如果你没有登录,不管你打开的哪页,都会进入指定的登录页面,登录完成后,又能返回你指定的页...
authentication mode="Forms" >
<forms loginUrl="login.aspx"></forms>
</authentication>
-----------------------------上面是身份验证的
-----------------下面是授权的
<authorization>
<deny users="?"/>
</authorization>
将已验证身份的用户重定向回最初请求的 URL
FormsAuthentication.RedirectFromLoginPage方法,会自动完成很多功能的。如完成生成身份验证票,写回客户端,浏览器重定向等一系列的动作。当然完成这些功能并不是只有FormsAuthentication.RedirectFromLoginPage方法才能办到,相反如果需要带角色信息的验证则只能采用其他办法。
我门可采用手动添加身份验证票
1.FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket (1,"coffee",DateTime.Now, DateTime.Now.AddMinutes(20), false,UserRoles,"/") ;
2.加密序列化
string HashTicket = FormsAuthentication.Encrypt (Ticket) ;
3.生成cookie
HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket) ;
4.身份验证票Cookie输出到客户端
Response.Cookies.Add(UserCookie)
5.重定向
Response.Redirect (Context.Request["ReturnUrl"]) ;