对文本框中密码加密,对web.config中字符串的加密,身份验证

来源:互联网 发布:数据仓库与数据挖掘pdf 编辑:程序博客网 时间:2024/05/16 10:35

在ASP.NET中对文本框中字符串加密

//給輸入內容加密
TextBox2 .Text =FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox1 .Text ,"MD5");//还有一种加密方式("SHA1"))

 

程序-VS2005-VS2005 Tools-命令提示;(第三个参数是你项目所在的文件)

 

//加密
aspnet_regiis.exe -pef "connectionStrings" "D:/MyBookShop/Web" -prov "DataProtectionConfigurationProvider"

//解密
aspnet_regiis.exe -pdf "connectionStrings" "D:/MyBookShop/Web"

 

!!!!这个操作只能在同一台电脑上操作!!!!!

!!!!Web项目不能包含中文字符及特殊字符!!!

 

----身份验证,验证用户是否拥有相应的身份

----权限验证,控制各种身份的用户拥有身份的权限

表单身份验证示例:在<system.web>中进行配置

 

通过 <authentication> 节可以配置 ASP.NET 使用的
            安全身份验证模式,
            以标识传入的用户。
        -->
  <authentication mode="Forms">
   <forms name ="huiyuan" loginUrl ="Login.aspx" timeout="60"></forms>
  </authentication>
  <authorization>
   <!--<deny users ="?"/>
   <deny users="*"/>-->
   <allow roles ="admin"/>
  </authorization>

 

可以在后台代码给用户创建身份(示例):

 if ((txtUserName.Text.Trim() ==lu.UserName) && (txtPwd.Text.Trim() == lu.UserPassword))
        {
            FormsAuthentication.SetAuthCookie(txtUserName .Text .Trim (),true);//创建身份(基于cookie的身份验证)
            //FormsAuthentication.SignOut();//删除身份验证
            string url = Request.QueryString["ReturnUrl"].ToString();//获取之前访问的页面
            Session["typeid"] = lu.TypeId.TypeId;
            Response.Redirect(url);//取得验证后跳转到之前的页面
        }

 

自定义错误的设置(示例):在<system.web>中进行配置

        <customErrors mode="On" defaultRedirect="Error.htm">
            <error statusCode="403" redirect="NoAccess.htm" />//没有权限的页面
            <error statusCode="404" redirect="FileNotFound.htm" />//页面不存在时的页面
        </customErrors>

 

 

原创粉丝点击