ASP.NET登录问题

标签 asp.net redirect authentication

我似乎无法在asp.net中设置身份验证系统 我有登录系统的代码:

protected void btnlogin_Click(object sender, EventArgs e)
{
    PageUser myUser = new PageUser();
    if (myUser.AuthenticateUser(txtUsername.Text, txtPassword.Text))
    {
        // entry found

        HttpCookie myCookie;

        DateTime now = DateTime.Now;

        myCookie = new HttpCookie("UserName");
        myCookie.Value = myUser.UserName;
        myCookie.Expires = now.AddMinutes(30);
        Response.Cookies.Add(myCookie);

        myCookie = new HttpCookie("LoginID");
        myCookie.Value = myUser.UserLoginID.ToString();
        myCookie.Expires = now.AddMinutes(30);
        Response.Cookies.Add(myCookie);

        lblResult.Visible = false;

        FormsAuthentication.SetAuthCookie(myUser.UserName + " " + myUser.UserLoginID.ToString(), true);

        Response.Redirect("AdminView.aspx");
    }
    else
    {
        // entry not found
        lblResult.Text = "<b>Invalid logon attempt<b>";
        lblResult.ForeColor = System.Drawing.Color.FromName("Red");
        lblResult.Visible = true;
    }
}

身份验证方法工作正常,但是当我不登录时,它仍然允许我重定向 AdminView,即使该人没有登录。 我遇到困难的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {

    }
    string userName = "";
    string[] splits;
    try
    {
        if (this.Page.User.Identity.IsAuthenticated)
        {
            splits = this.Page.User.Identity.Name.Split(new char[1] { ' ' });
            userName = splits[0] + " " + splits[1];

        }
        else
        {
            Response.Redirect("default.aspx");
        }
        txtLoggedInUser.Text += " - " + userName;
    }
    catch
    {
        Response.Redirect("default.aspx");
    }
}

我不知道如何编写此代码,以便当用户尝试访问管理页面时将其重定向回登录页面。

最佳答案

将未经身份验证的用户限制为 AdminView.aspx页面,您必须将以下内容添加到configurationweb.config 部分文件。

<location path="AdminView.aspx">
<system.web>
    <authorization>
        <deny users="?"/>               
    </authorization>
</system.web>

<deny users="?"/>意思是unauthenticated用户将无法访问文件/文件夹AdminView.aspx

关于ASP.NET登录问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6441151/

相关文章:

redirect - 用户登录后如何重定向回用户想要访问的页面?

grails - 没有重定向的Grails调用函数

php - 使用 PHP 从 Microsoft Teams 自定义机器人验证 HMAC

c# - Unity玩游戏服务认证失败

javascript - 在 gridview 行鼠标悬停时,我想在另一个标签中显示行数据

asp.net - 如何在服务器端禁用asp.net中的DropDownList选项?

asp.net - Response.Redirect ("SomeURL", false) 的有效用途是什么?

c# - 在现有 ASP.NET WebForms 站点中添加具有根路径的第二语言支持

tomcat - AWS ELB 和 Tomcat7 服务器的 HTTP 到 HTTPS 重定向

c# - 用于超大型应用程序的 ASP.NET 自定义成员身份提供程序