我正在努力让会员服务Provider 工作.
到目前为止,我已经:
<asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate">
</asp:Login>
打电话:
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
if(Membership.ValidateUser(Login1.UserName, Login1.Password))
{
Response.Redirect("/admin/default.aspx");
// Set the user as logged in?
}
}
如果我输入了正确的登录/密码,则ValidateUser函数返回TRUE.所以我的问题是:如何将用户设置为已登录?
我正在我的页面中测试这一点:
protected void Page_Load(object sender, EventArgs e)
{
if ( Membership.GetUser()==null)
{
Response.Redirect("/admin/login.aspx");
}
// else "you are logged in, congratulations"
}
I would have used the default functions, but it is just not working and a google search made me think that I will save time by actually recoding all that myself.个
什么都行!
EDIT:关于被接受的答案,它是"如何将用户设置为已登录"的正确答案,可以正常工作.它没有解决我的具体问题,但只是其中的一部分.如果你看这些 comments ,你会发现有趣的提示.
编辑2和解决方案:好的,多亏了所有的 comments ,我终于解决了这个问题.以下是我所做的,比我预期的要简单:
判断登录状态的页面:
protected void Page_Load(object sender, EventArgs e)
{
if ( !Request.IsAuthenticated)
{
Response.Redirect("/admin/login.aspx");
}
注销:
protected void LoginStatus1_Logout(object sender, LoginCancelEventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("/admin/login.aspx");
}
}
网状物配置:
<authentication mode="Forms" />
登录:
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
if(Membership.ValidateUser(Login1.UserName, Login1.Password))
{
FormsAuthentication.SetAuthCookie(Login1.UserName, true);
Response.Redirect("/admin/default.aspx");
}
}