简体   繁体   English

用户未登录时(asp)重定向到登录页面

[英]Redirect to login-page when user is not logged in (asp)

I have a login-page where users can log in. When they logging in with correctly details they are sent to an main admin-page. 我有一个登录页面,用户可以在其中登录。使用正确的详细信息登录时,他们将被发送到主管理页面。 If they cant log in they are staying on the login-page. 如果他们无法登录,他们将停留在登录页面上。 What I want to do is, if a random user, type in the URL for an admin-page when they are not logged in they are redirecting to the login-page. 我想做的是,如果是随机用户,则在他们未登录时将输入管理员页面的URL,而他们正在重定向到登录页面。

I have understood that I have to do it in the masterpage or webconfig!?! 我了解我必须在母版页或webconfig中完成!!! I have a main admin-page and some other admin-pages. 我有一个主要的管理页面和其他一些管理页面。

Any tips? 有小费吗?

ASP.Net has ASP.Net Identity which basically is membership system. ASP.Net具有ASP.Net Identity ,基本上是成员资格系统。

It'll be over kill for a simple website with couple of pages. 一个简单的只有几个页面的网站将被淘汰。 If you just want a very basic feature without overhead, you could just look at - 如果您只是想要一个非常基本的功能而又没有开销,则可以看看-

I tried to insert this into my webconfig: 我试图将其插入到我的webconfig中:

<authentication mode="Forms">
    <forms loginUrl="InnUtlogging.aspx" timeout="2880"/>
  </authentication>

here is my code for the "login"-button (on the login-page); 这是我的“登录”按钮的代码(在登录页面上);

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("select * FROM Ansatt WHERE epost='" + brukernavn.Text + "' and passord='" + passord.Text + "'");
        cmd.Connection = con;
        int OBJ = Convert.ToInt32(cmd.ExecuteScalar());

        if (OBJ > 0)

            {
            Session["name"] = brukernavn.Text;
            Response.Redirect("KunstnerAdmin.aspx");
        }
        else
            {
                melding.Text = "Feil brukernavn/passord";
            }
        if (brukernavn.Text == "")
        {
            melding.Text = "Du må fylle inn brukernavn";

        }
        if (passord.Text == "")
        {
            melding.Text = "Du må fylle inn passord";
        }
        }

But actually I want to check if user is logged in in the master-page. 但是实际上我想检查用户是否在母版页中登录。 Is there something I can do in the masterpage to activate the forms authentication? 我可以在母版页中做些什么来激活表单身份验证?

暂无
暂无

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

相关问题 以编程方式登录网站并将用户重定向到登录页面? - Programmatically login to a website and redirect the user to the logged in page? 将Cookie传递到登录页面 - Passing cookie to login-page 用户已经登录后,如何重定向到未授权页面而不是登录页面? - How do I redirect to a not-authorized page instead of the login page when the user is already logged in? 如何使用后退按钮成功登录后阻止用户返回登录页面 - How to prevent user from going back to the login-page after successful login using back button 如果未登录,则重定向用户登录 ASP.NET C# - Redirect user to login if not logged in ASP.NET C# 当用户未登录 ASP.NET MVC 时重定向到页面的最简单方法是什么? - Which is the simplest way to redirect to page when user is not logged in in ASP.NET MVC? asp.net用户登录但返回登录页面 - asp.net user being logged in but being returned to login page ASP.net:重定向到欢迎页面后,当用户成功登录后,返回注册页面时,缓存不清除 - ASP.net: Cache doesn't clear when backward to registration page after redirect to welcome page and user successfully logged-in 我在ASP.net中登录时如何根据用户重定向页面? - How to redirect the page according to user when I login in ASP.net? 如果用户已经登录 Blazor 服务器,如何将身份登录页面设置为启动页面并将用户重定向到仪表板 - How To Make Identity Login Page As Startup Page And Redirect User to Dashboard If User Is Already Logged-in In Blazor Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM