身份验证(禁止直接访问页面)

在Web.config中写入下面代码,就可以实现不登陆无法访问其他页面的效果 

    (一)  

    <system.web>

        <compilation debug="true" targetFramework="4.0" />
      <authentication mode="Forms">
        <forms loginUrl="WebLogin.aspx" defaultUrl="Index.aspx"/>  //注:WebLogin.aspx未登录页面
      </authentication>
      <authorization>
        <!---拒绝所有匿名用户访问项目下的所有文件-->
        <!--<deny users="?"/>-->
        <!--设置所有用户都可以访问项目下的所有文件-->
        <allow users="*"/>

      </authorization>

    </system.web>

(二)
可以指定允许登录其他页面,不允许登陆管理者页面 admin为文件夹,所有管理者的页面都在文件夹admin中
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
      <authentication mode="Forms">
        <forms loginUrl="WebLogin.aspx" defaultUrl="Index.aspx"/>
      </authentication>
      <authorization>
        <!---拒绝所有匿名用户访问项目下的所有文件-->
        <!--<deny users="?"/>-->
<!--设置所有用户都可以访问项目下的所有文件-->
        <allow users="*"/>
      </authorization>
    </system.web>

//在添加上下边的代码
  <location path="admin">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>
 

</configuration>

在登陆页面的后台的判断中写如下代码:

FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, true);     //注:txtUserName为输入用户名,若后边是true时,将会记住登录用户名和密码,为false时则不会记住,需要每次都登陆

原文地址:https://www.cnblogs.com/duanlinlin/p/3134498.html