您的位置:首页 > 编程语言 > ASP

ASP.NET(c#) 中通过配置配置authentication 来验证控制 Login 登录

2014-12-30 16:33 537 查看
本文讲述如何在web.cofig里通过配置authentication节点来控制权限登录。其实关键点就是配置Web.config 文件



一,配置Web.config

首先在 Web.config <authentication> 里将身份验证模式更改为Forms(窗体)。具体代码如下
<system.web>
  <!--拒绝匿名用户访问-->
  <authentication mode="Forms">
          <forms loginUrl="Default.aspx" defaultUrl="Admin.aspx" name=".ASPXFORMSAUTH">
          </forms>
  </authentication>
  <authorization>
      <deny users="?"/>
      <!--<allow users = "*" />-->
  </authorization>
</system.web>
<location path="progressbar.aspx">
  <system.web>
    <authorization>
      <allow users="?" />
    </authorization>
  </system.web>
</location>
loginUrl:用户没有登录,跳转到的登录页面

cookieless:用户登录后的cookieName(可选)

defaultUrl:正确登录之后,在没有指向页的时候,弄人跳转的页面(可选)

authorization节点功能是拒绝匿名用户访问该文件夹目录下所有文件功能

到目前为止,除了Default.aspx和progressbar.aspx页面,你访问其他的页面,都会自动跳转到Default.aspx登陆页面,要求你先登录。

如果有其它异常问题,web.config中通过外置location 元素来锁定配置设置

二,页面后台的代码应用

protected void btnLogIn_Click(object sender, EventArgs e)
    {
        string userName = "", password = "";

        using (StreamReader sr = File.OpenText("D:\\Temp\\user info.txt"))
        {
            string input = null;
            while ((input = sr.ReadLine()) != null)
            {
                if (input == userName)
                {
                    input = sr.ReadLine();
                    if (input == password)
                    {
                        //other else options
                    }
                }
            }
        }
    }
    protected void User_Logout()
    {
        Session.Abandon();
        //删除用户票据
        FormsAuthentication.SignOut();
        //重新定向到登陆页面
        FormsAuthentication.RedirectToLoginPage();
    }
其中User_Logout()方法是注销登录用

其它可参考我之前的文章: http://blog.csdn.net/jintougao/article/details/9281275
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐