[英]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.