简体   繁体   English

如果未登录,则重定向用户登录 ASP.NET C#

[英]Redirect user to login if not logged in ASP.NET C#

I'm trying to develop web app in ASP.NET web forms using C# and i want to Redirect user to login if not logged but when i try to login i'm not able to redirect to the dashboard.aspx page i just found my self in login page please any one can help me:)我正在尝试使用 C# 在 ASP.NET web forms 中开发 web 应用程序,如果未登录,我想重定向用户登录,但是当我尝试登录时,我无法重定向到 dashboard.aspx 页面,我刚刚发现自己在登录页面请任何人都可以帮助我:)

 protected void Page_Load(object sender, EventArgs e)
{
    if (Session["username"] == null)
    {
        Response.Redirect("login.aspx", true);
        return;
    }
    //else
    //{
    //    Response.Redirect("dashbord.aspx", true);
    //}
}

login.aspx登录.aspx

string user = Request["login1"];
string code = Request["pass"];
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from admin where username  = '"+user+"' and password = '"+ code + "'";
cmd.ExecuteNonQuery();
Session["username"] = user;
Session.Clear();
Session.Abandon();
Session.RemoveAll();
Response.Redirect("dashbord.aspx");

You should try and use a built in security provider.您应该尝试使用内置的安全提供程序。 (even the simple FBA and role providers is better then this). (即使是简单的 FBA 和角色提供者也比这更好)。 If a user hits a page that is secured or requires a logon, then IIS will jump to logon page, and then automatic re-direct to where you were going in the first place.如果用户点击安全页面或需要登录,则 IIS 将跳转到登录页面,然后自动重定向到您首先要去的地方。 So, you really don't want to handle this on your own, since then simple efforts to set a web page is in a secured folder becomes easy.所以,你真的不想自己处理这个,因为这样简单的努力将 web 页面设置在安全文件夹中就变得容易了。

See this recent post of mine - as to how this works, or at least should work.请参阅我最近发表的这篇文章 - 关于它是如何工作的,或者至少应该如何工作。 If you don't adopt a built in security system, then you will be for ever more trying to write code on near every web page to deal with the near "unlimited" ways that users can type in any old URL, and that's a challenge.如果您不采用内置安全系统,那么您将永远尝试在几乎每个 web 页面上编写代码来处理用户可以输入任何旧 URL 的近乎“无限”方式,这是一个挑战.

Authorization issues in asp page VB.NET asp页面中的授权问题 VB.NET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM