我正在努力让会员服务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");

    }
}

推荐答案

在打Response.Redirect("/admin/default.aspx");之前把这个放进Login1_Authenticate

FormsAuthentication.SetAuthCookie("username", true);

Asp.net相关问答推荐

Windows 命令行中的/p:是什么意思

强制从 /bin 而不是 GAC 加载程序集?

如何防止 IISExpress 和我的网站文件夹弄乱我的文档文件夹?

RNGCryptoServiceProvider - 随机数审核

Web 服务器配置为不列出此目录的内容. asp.net vs 2012 错误?

log4net 记录所有未处理的应用程序错误

如何检测 ASP.net 应用程序中的 SqlServer 连接泄漏?

使用 Elmah 处理 Web 服务中的异常

会话变量保存在哪里?

Page.IsValid 是如何工作的?

MVC5 身份验证中的......与主域之间的信任关系失败

无法序列化会话状态

如何在 C# 应用程序中调用 VBScript 文件?

如何删除asp.net中的特定会话?

如何从 ASP.NET 中的数据表/数据视图中 Select 前 n 行

如何将参数传递给 ASP.NET MVC 2 中的自定义 ActionFilter?

System.Reflection.Assembly.LoadFile 锁定文件

在 Application_BeginRequest 中设置会话变量

将列表转换为 json 格式 - 快速简便的方法

如何在asp.net的GridView中按列名而不是按索引获取单元格值