打印本文 打印本文 关闭窗口 关闭窗口
ASP.NET Authentication Provider
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2324  更新时间:2009/4/23 10:48:01  文章录入:mintao  责任编辑:mintao
SHA1:用SHA1的哈希分类保存密码。验证时将用SHA1算法对用户密码进行散列,然后同该值进行比较。

MD5:同SHA1类似,只是使用不同的算法。

当使用SHA1MD5时还需要一个专门的APIHashPasswordForStoringInConfigFile)来执行加密,然后结果因保存到config.web文件中。具体可以参考下面的链接:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconformsauthenticationutilities.asp

 

2.   然后点击或在login.aspx 文件的Login按钮的Click的事件处理程序中写入下面的代码:

    void Login_Click(Object sender, EventArgs E) {

 

if ( FormsAuthentication.Authenticate( UserEmail.Value, UserPass.Value) )

{

        FormsAuthentication.RedirectFromLoginPage(UserEmail.Value, PersistCookie.Checked);

      }

      else {

        Msg.Text = "Invalid Credentials: Please try again";

      }

    }

    结果和上一个相同,但这次我们把判断交给了ASP.NET AP ,我们只传递了UserEmail,UserPass两个参数,Authenticate方法将完成Authentication过程,这个用户将和我们在config.web<user></user>中设置的相同。

 

Login Out

 

    MS文档《Forms Authentication Using An XML Users File》展示了另外一种获取用户名和密码的方式,这种方式为了安全,用户和密码被放在一个单独的XML文件中。具体参考下面的链接:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconformsauthenticationutilities.asp

同样的道理你可以象你以前做得一样将用户和密码放到数据库表中,比如:

    void Login_Click(Object sender, EventArgs e) {

         if(Page.IsValid)

            {

            SQLDataReader dr;

           

            // Connect to the database

            SQLConnection cn = new SQLConnection("server=localhost;

            uid=myPassport;pwd=123;database=clientPassword;");

cn.Open();

            // Create a command to get the question

            SQLCommand cmdQuestion = new SQLCommand("SELECT Password;

            FROM Users WHERE Email = ''''" + UserEmail.Value + "''''", cn);

            

            cmdQuestion.Execute(out dr);

            if(dr.Read())

               if(dr["Password"].ToString() == UserPass.Value)

                  FormsAuthentication.RedirectFromLoginPage(UserEmail.Value, PersistCookie.Checked);

               else

                  Msg.Text = "Invalid password. Please try again ";

            else

               Msg.Text = "Email address not found.";

            }

     }

   

上一页  [1] [2] [3] [4]  下一页

打印本文 打印本文 关闭窗口 关闭窗口