asp.net网站登录的一些资料。

网站用户注册和登录,一直是我做项目的拦路虎,由于底子很浅,自己用session和cookie做一个用户注册和登录机制,很难有的东西真的不懂,曾经也做过一个,但效果不好。现在asp.net2.0以上,提出membership用户管理机制,大方便了初学者的网站开发。但在小项目里,自己搞一个用户注册和登录的机制,membership还是很大,(光数据库就10M)手头上有Access的membership的版本,抽空一定要研究一番。

下面对我所看到的一些资料做一个总结和整理吧。

园子里有

使用Forms Authentication实现用户注册、登录 (一)基础知识

http://www.cnblogs.com/AndersLiu/archive/2008/01/01/forms-authentication-part-1.html

使用Forms Authentication实现用户注册、登录 (二)用户注册与登录

http://www.cnblogs.com/AndersLiu/archive/2008/01/01/forms-authentication-part-2.html

使用Forms Authentication实现用户注册、登录 (三)用户实体替换

http://www.cnblogs.com/AndersLiu/archive/2008/01/01/forms-authentication-part-3.html

挺好。

关于Membership用户密码的资料

http://www.cnblogs.com/AndersLiu/archive/2007/12/28/encode-password-with-salt.html

1、在web.config里配置好验证信息

<authentication mode="Forms">

     <forms

         name=".ASPXAUTH"

         loginUrl="login.aspx"

         defaultUrl="default.aspx"

         protection="All"

         timeout="30"

         path="/"

         requireSSL="false"

         slidingExpiration="false"

         enableCrossAppRedirects="false"

         cookieless="UseDeviceProfile"

         domain=""

     />

</authentication>

2、用户验证可以

   // 判断用户是否已登录。
  if(HttpContext.Current.User.Identity.Name == "")

       {

           Response.Write("您已经登录!");

        }
        else
        {
            Response.Redirect("~/login.aspx");
        }

System.web.security.FormsAuthentication类为web应用程序管理Forms身份验证提供了一系列的服务

(普通登录页)GetAuthCookie(UserName,PersistCookie) 返回FormsAuthenticationTicket提供票证

(需要用户保存点信息)SetAuthCookie(UserName,PersistCookie) 第一个方法后生成的Cookie写到客户端的Cookie集中去

(用户重定向)RedirectFromLoginPage(UserName,persistCookie) 在第二个方法后,重定向要访问的页面。

 

3、用户登录可以用

 

if (tbUser.Text.Trim() == "user" && tbPwd.Text.Trim() == "pwd")//数据库中取值
        {
            //登录到当前页的场景中用
            //FormsAuthentication.SetAuthCookie(tbUser.Text.Trim(), CheckBox1.Checked);
            ////string url = FormsAuthentication.GetRedirectUrl(User.Identity.Name, CheckBox1.Checked);//返回原始请求页
            //Response.Redirect("~/default.aspx");

            //-----------------------------------------------改变authCookie
            string UserDataStr = tbUser.Text.Trim() + "|" + tbPwd.Text.Trim();
            HttpCookie authCookie = FormsAuthentication.GetAuthCookie(tbUser.Text.Trim(),CheckBox1.Checked);
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
            FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, UserDataStr);
            authCookie.Value = FormsAuthentication.Encrypt(newTicket);
            Response.Cookies.Add(authCookie);
            //string url = FormsAuthentication.GetRedirectUrl(User.Identity.Name, CheckBox1.Checked);//返回原始请求页
            Response.Redirect("~/mypage.aspx");
        }
        else
        {
            lblInfo.Text = "用户名和密码错误!";
        }

4、用户注销

FormsAuthentication.SignOut();
  Response.Redirect(Request.RawUrl);

 

posted @ 2009-02-19 09:39  teacherzj  阅读(249)  评论(0编辑  收藏  举报