2016-02-11 42 views
1

我想,如果他们没有登录的任何用户受到限制限制用户。 想,如果他们试图通过粘贴他们仍然被重定向到登录页面的链接访问任何页面。访问任何网页,而不登录

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 [email protected] and [email protected]", 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"); 
    } 
} 

回答

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

+0

我不想限制每个文件夹分开。 –

+0

所以你可以使用第二个片段(所有页面都用'')。 – feeeper

+0

我如何知道用户已通过身份验证?没有会议 –

1

您可以通过ASP会话实现它.net:

包括下面的命名空间:

用户像如下

登录页面登录成功之后创建一个会话。其中要保护应该有这个如下每一页

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString); 
    con.Open(); 

    SqlCommand cmd = new SqlCommand("select * from Employee where UName [email protected] and [email protected]", 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... 
    } 

现在:

using System.Web.SessionState;

用户后输入的用户名和密码

protected void Page_Load(object sender, EventArgs e) 
     { 
      if (Session["Sid"] == null) 
      { 
       Response.Redirect("Login.aspx"); 

      } 
     } 

网络。配置

<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="120" /> 

我希望这可以帮助...

+0

烨感谢@Sankar拉吉 –

+0

很乐意帮助你:) – Sankar

+0

这是在asp.net传统的方法是什么?任何其他好的方法,没有会议? – Sak