我希望任何用户在未登录的情况下受到限制。 假设他们试图通过粘贴链接来访问任何页面,他们仍被重定向到登录页面。
LoginPage
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Employee where UName =@username and UPassword=@password", con);
cmd.Parameters.AddWithValue("@username", UName.Text);
cmd.Parameters.AddWithValue("@password", UPassword.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Response.Redirect("Details.aspx");
}
}
答案 0 :(得分:1)
我认为使用<allow>
中的web.config
部分可以帮助您:
<!--Deny access for 'images' folder-->
<location path="images">
<system.web>
<authorization>
<allow users="?"/> <!--A question mark (?) denies anonymous users-->
</authorization>
</system.web>
</location>
<!--Will deny anonymous users for all pages-->
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
更多相关信息:https://msdn.microsoft.com/en-us/library/acsd09b0(v=vs.85).aspx
答案 1 :(得分:1)
你可以通过asp.net中的会话实现它:
在用户成功登录后创建会话,如下所示
登录页面
包含以下命名空间。
using System.Web.SessionState;
:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Employee where UName =@username and UPassword=@password", con);
cmd.Parameters.AddWithValue("@username", UName.Text);
cmd.Parameters.AddWithValue("@password", UPassword.Text);
//Blah,Blah,Blah...
if(user=authenticated user) //your condition goes here
{
session["Sid"]=Session.SessionID;
//Blah,Blah,Blah...
}
现在,您想要保护的每个页面都应该如下:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Sid"] == null)
{
Response.Redirect("Login.aspx");
}
}
在 web.config :
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="120" />
我希望这会有所帮助......