我的解决方案资源管理器(如果帮助):http://i.stack.imgur.com/BdhDz.png
当我点击帐户按钮( href到account.aspx)时,它会因为没有授权而将我重定向到login.aspx。
在我的login.aspx.cs上(在他们点击登录后),我
Session["hi"] = "hello";
HttpCookie cookie = new HttpCookie("username");
cookie.Value = UName.Text;
Response.Cookies.Add(cookie);
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(UName.Text, false);
在account.aspx.cs上
if (Session["hi"] != null)
{
Literal1.Text = Session["hi"].ToString();
Literal1.Text += "<br><br><br>";
}
为什么它没有显示任何东西呢?我尝试了各种方式来显示这个会话,但是没有工作,由于某种原因,会话“hi”是空的。
发布于 2014-04-17 10:58:24
根据您最后的评论,您需要将AuthenticationSection.Mode性质设置为mode="Forms"
。
默认情况下,它是窗口模式。
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx"
timeout="2880"
cookieless="UseCookies" />
</authentication>
如何创建主体对象
一旦通过身份验证的用户被请求一个页面,您就需要从cookie中检索auth票证,并创建一个主体对象。
Global.asax.cs
void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpCookie decryptedCookie =
Context.Request.Cookies[FormsAuthentication.FormsCookieName];
FormsAuthenticationTicket ticket =
FormsAuthentication.Decrypt(decryptedCookie.Value);
var identity = new GenericIdentity(ticket.Name);
var principal = new GenericPrincipal(identity, null);
HttpContext.Current.User = principal;
Thread.CurrentPrincipal =HttpContext.Current.User;
}
用法
if (User.Identity.IsAuthenticated)
{
}
https://stackoverflow.com/questions/23139381
复制相似问题