我正在尝试让会员提供者工作。
到目前为止,我有:
<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"
}
我会使用默认功能,但它只是不起作用,谷歌搜索让我认为我可以通过自己重新编码来节省时间。
任何事情都会有所帮助!
编辑:关于接受的答案,它是“如何将用户设置为已登录”的正确答案,并且工作正常。它没有解决我的具体问题,但只是其中的一部分。想如果你看一下评论,你会发现有趣的指针。
编辑 2 和解决方案:好的,感谢所有评论,我终于解决了。这是我所做的,它比我预期的要简单:
检查登录状态的页面:
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");
}
}