0

我已经创建了 3 个文件夹来管理 ASP.NET 中的用户,并且我还创建了 3 个角色,分别名为官员、用户和管理员。现在基于以下代码,用户可以重定向到特定页面,但现在的问题是我可以'登录后看不到使用loginName添加的用户名 ,LoginStatus 会自动从注销更改为登录。似乎用户没有登录并要求再次登录。(有趣的问题......)

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    if (Membership.ValidateUser(Login1.UserName, Login1.Password))
    {
        //Perform setting cookie information

        e.Authenticated = true;
        if (Roles.IsUserInRole(Login1.UserName, "r_admin"))
        {
            Response.Redirect("admin/default.aspx");
        }

        if (Roles.IsUserInRole(Login1.UserName, "r_officer"))
        {
            Response.Redirect("~/officer/default.aspx");
        }
        if (Roles.IsUserInRole(Login1.UserName, "r_user"))
        {
            Response.Redirect("~/user/default.aspx");
        }


    }
4

1 回答 1

0

尝试将 Response.Redirect 指令移动到 LoggedIn 事件,而不是 Authenticate 事件。

在您的登录控件上添加 Loggedin 事件,如下所示:

  <asp:Login id="Login1" runat="server" OnLoggedIn="Login1_OnLoggedIn"></asp:Login>

在后面的代码中:

protecetd void Login1_OnLoggedIn(object sender, EventArgs e)
{
     if (Roles.IsUserInRole(Login1.UserName, "r_admin"))
    {
        Response.Redirect("admin/default.aspx");
    }

    if (Roles.IsUserInRole(Login1.UserName, "r_officer"))
    {
        Response.Redirect("~/officer/default.aspx");
    }
    if (Roles.IsUserInRole(Login1.UserName, "r_user"))
    {
        Response.Redirect("~/user/default.aspx");
    }
}

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.loggedin.aspx

于 2014-03-10T13:06:42.130 回答