zoukankan      html  css  js  c++  java
  • ASP.NET Form表单验证

    一: ASP.NET 的安全认证模式
    Windows, Forms, Passport, None

    二: 修改验证模式
    修改Web.config <system.web>

        <!--修改验证模式为Forms-->
    <authentication mode="Forms">
    <forms loginUrl="~/Login.aspx" name="HotelUser" defaultUrl="Default.aspx"></forms>
    </authentication>
    <!--禁止匿名登陆-->
    <authorization>
    <deny users="?"/>
    </authorization>

    三: 用户登陆,发身份验证票
    FormsAuthentication.FormsCookieName ~ 就是上面的HotelUser

    //登陆成功后,返回请求页面
    a:System.Web.Security.FormsAuthentication.RedirectFromLoginPage(FormsAuthentication.FormsCookieName,false);

    //发验证票,到指定页面
    b:System.Web.Security.FormsAuthentication.SetAuthCookie(FormsAuthentication.FormsCookieName,false);
    Response.Redirect("Default.aspx");

    四: 用户退出
    System.Web.Security.FormsAuthentication.SignOut();

    五: 用户是否通过验证
    User.Identity.IsAuthenticated //如果通过验证,或是有Cookie存在 值为True,否则为False

    六: Web.config 作用范围
    0: machine.config 的设置 作用整个机器所有目录及其目录下所有文件.   -->子随父姓
    1: Web.config 的设置 作用于所在目录的所有文件及其子目录下所有文.   -->子随父姓
    2: 子目录下的 Web.config 设置将覆盖由父目录继承下来的设置          -->将在外,军命有所不受

    七: 设置某个文件夹或文件的访问权限
    1:在相应文件夹下 建立web.config文件
    <authorization>
    <deny users="?"/>   //这里设置访问权限
    </authorization>

    2: 在根目录web.config下设置整个站点 所有文件夹 和 文夹的访问权限

    <configuration>
    //目录文件夹1
    <location path ="Public">    //<location path ="Public/Default.aspx"> 为某个文件设置访问配置
    <system.web>
    <authorization>
    <allow users="*"/>
    </authorization>
    </system.web>
    </location>

       //目录文件夹2
    <location path ="ManageSys">
    <system.web>
    <authorization>
    <allow users="Admin"/>
    <allow users="WF"/>
    <allow users="FY"/>
    <deny users="*"/>        
    </authorization>
    </system.web>
    </location>

    //原根目录web.config 配置
    <system.web>
    <authentication mode="Forms">
    <forms loginUrl="~/Login.aspx" name="HotelUser" defaultUrl="Default.aspx"></forms>
    </authentication>   
    <authorization>
    <deny users="?"/>
    </authorization>
    </system.web>
    </configuration>

    八:单点登陆
    1:获取机器key 生成密钥

    <密钥生成方法>
    protected void btn_OK_Click(object sender, EventArgs e)
    {
    string decStr = this.CreateKeyString(int.Parse(this.TextBox1.Text));
    string valStr = this.CreateKeyString(int.Parse(this.TextBox2.Text));
    this.TextBox3.Text = string.Format("<machineKey validationKey=\"{0}\" decryptionKey=\"{1}\" validation=\"SHA1\"/>", valStr, decStr);
    }
    /// <summary>
    /// 生成加密型强随机 Key 值
    /// </summary>
    /// <param name="i">Key 的有效长度:
    /// decryptionKey 的有效值为 8 或 24;
    /// validationKay 的有效值为 20 至 64
    /// </param>  
    private string CreateKeyString(int i)
    {
    System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); //加密随机数生成器
    byte[] bt = new byte[i];
    rng.GetBytes(bt);//用加密型强随机值序列填充字节数组
    System.Text.StringBuilder str = new System.Text.StringBuilder();
    for (int j = 0; j < i; j++)
    {
    str.Append(string.Format("{0:X2}", bt[j])); //转换成大写的十六进制文本
    }
    return str.ToString();
    }
    2:在要实现单点登陆的项目根web.config中添加密钥;

    a) 两个项目Web.cinfig的<machineKey> 节点确保以下几个字段完全一样:validationKey 、decryptionKey 、validation
    b) 两个项目的 Cookie 名称必须相同,也就是 <forms> 中的 name 属性,这里我们把它统一为 name ="UserLogin"    //name="www.wf.com"
    c) 注意区分大小写
    d) 登陆页面整合到统一登陆点登陆   比如:loginUrl="www.wf.com/login.aspx", 在登陆页面发放验证票    


    //项目网站1
    <system.web>
    <machineKey validationKey="B3D04DE25D02B63898851BD799F5E8DBE7CE043B9A09AC2E5ACE14BD9C84717E3731837A76923B327340945010C58C31" decryptionKey="9EBAA26A9E9424994CE2C0A4C0EA5B20" validation="SHA1"/>  
    <authentication mode="Forms">
    <forms name="UserLogin" loginUrl="~/Login.aspx"></forms>
    </authentication>
    <authorization>
    <deny users ="?"/>
    </authorization>  
    </system.web>

    //项目网站2
    <system.web>
    <machineKey validationKey="B3D04DE25D02B63898851BD799F5E8DBE7CE043B9A09AC2E5ACE14BD9C84717E3731837A76923B327340945010C58C31" decryptionKey="9EBAA26A9E9424994CE2C0A4C0EA5B20" validation="SHA1"/>  
    <authentication mode="Forms">
    <forms name="UserLogin" loginUrl="~/Login.aspx"></forms>
    </authentication>
    <authorization>
    <deny users ="?"/>
    </authorization>  
    </system.web>

          3: 给用户发放Cookie
    <1>: 一次登陆后,给各个站点同时发放Cookie认证。
    <2>: 一次登陆后,根据用户选择性的发放Cookie认证。

    九: Cookie
    1: 普通Cookie
    protected void Button1_Click(object sender, EventArgs e)
    {
    HttpCookie ck = new HttpCookie("str");
    ck.Expires.AddDays(1);
    ck["rr"] = "str_rr_";
    ck["w1"] = "str_w1_";      
    Response.Cookies.Add(ck); 

    HttpCookie ckNex = new HttpCookie("Nex");
    ck.Expires.AddDays(1);       
    ck.value= "Nex_";      
    Response.Cookies.Add(ckNex);        
    }
    protected void Button2_Click(object sender, EventArgs e)
    {         
    TextBox1.Text = Request.Cookies["str"]["w1"].ToString() + Request.Cookies["str"]["rr"].ToString();       
    }

    2: 生成用户验证的Cookie    
    public void AuthenticationUsers(string userName)
    {
    FormsAuthenticationTicket tichet = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddHours(24), true, "");
    string hashTichet = FormsAuthentication.Encrypt(tichet);

                HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName);
    userCookie.Value = hashTichet;
    userCookie.Expires = tichet.Expiration;
    userCookie.Domain = FormsAuthentication.CookieDomain;
    HttpContext.Current.Response.Cookies.Add(userCookie);
    }

    这个Cookie 相当与 下面两发放的Cookie

    //登陆成功后,返回请求页面
    a:System.Web.Security.FormsAuthentication.RedirectFromLoginPage(FormsAuthentication.FormsCookieName,false);
    //发验证票,到指定页面
    b:System.Web.Security.FormsAuthentication.SetAuthCookie(FormsAuthentication.FormsCookieName,false);

  • 相关阅读:
    Samba 4.0 RC3 发布
    SymmetricDS 3.1.7 发布,数据同步和复制
    Express.js 3.0 发布,Node.js 的高性能封装
    GIFLIB 5.0.1 发布,C语言的GIF处理库
    jQuery UI 1.9.1 发布
    SVN Access Manager 0.5.5.14 发布 SVN 管理工具
    DynamicReports 3.0.3 发布 Java 报表工具
    HttpComponents HttpClient 4.2.2 GA 发布
    AppCan 2.0 正式发布,推移动应用云服务
    Ruby 2.0 的新功能已经冻结
  • 原文地址:https://www.cnblogs.com/jasonjiang/p/1763548.html
Copyright © 2011-2022 走看看