32

我正在尝试让会员提供者工作。

到目前为止,我有:

 <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");

    }
}
4

3 回答 3

41

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

FormsAuthentication.SetAuthCookie("username", true);
于 2009-05-26T19:32:25.597 回答
6

尝试将您的代码和 Gromer 的建议移至 LoggedIn 事件。

protected void Login1_LoggedIn(object sender, EventArgs e)
    {
        if(Membership.ValidateUser(Login1.UserName, Login1.Password))
        {
            FormsAuthentication.SetAuthCookie(Login1.UserName, true);
            Response.Redirect("/admin/default.aspx");
        }

    }

编辑:就像格罗默说的那样,只有在用户登录之后和他/她被重定向之前必须执行一些业务代码时才这样做。

编辑编辑:Visual Studio 将 Authenticate 事件描述为“调用以对用户进行身份验证”,这意味着在调用事件之前未对用户进行身份验证。因此,您无法确认用户已登录,因为他/她尚未通过身份验证。

于 2009-05-26T19:41:52.740 回答
1

虽然我不知道这会有多大帮助,但这是我用来区分管理员用户或普通用户的样板代码。对我很有用。

在您的登录页面上,可能 onclick 创建您的用户对象并使用此代码调用一些函数(UserRole 是具有您角色的枚举):

If admin Then 
            If role = UserRole.Admin Then
                RedirectFromLoginPage(username & "|" & userid, False)
                Return True
            Else
                Return False
            End If
        Else
            If String.IsNullOrEmpty(Current.Request.QueryString("ReturnUrl")) Then
                SetAuthCookie(username & "|" & userid, True)
            Else
                RedirectFromLoginPage(username & "|" & userid, True)
            End If
            Return True
        End If

在您的 web.config 中:

<location path="admin">
    <system.web>
        <authorization>
            <allow roles="Admin"/>
            <deny users="*"/>
        </authorization>
    </system.web>
</location>
.....
<system.web>
<authentication mode="Forms">
        <forms loginUrl="/registration/login.aspx" timeout="129600"/>
    </authentication>
    <authorization>
        <allow users="*"/>
    </authorization>
</system.web>

...如果你真的想要,在你的 Global.asax 页面中:

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
    If Request.IsAuthenticated Then
''
'get your roles for the current user'
''
 Dim userRoles() As String = Split(roles, "|")
        'Add the roles to the User Principal'
        HttpContext.Current.User = New GenericPrincipal(User.Identity, userRoles)
    End If
End Sub
于 2009-05-26T20:02:00.437 回答