.net Forms 验证-阿里云开发者社区

开发者社区> 安全> 正文

.net Forms 验证

简介: 这俩天在搞 Forms 验证。终于搞出来了。。。 web.config 配置:              。。。                    Login.aspx.cs    protected void LoginButton_Click(object sender,...

这俩天在搞 Forms 验证。终于搞出来了。。。

web.config 配置:

<authentication mode="Forms">
   <forms name="test" loginUrl="Login.aspx" defaultUrl="Main.aspx" timeout="4"/>
  </authentication>
  <authorization>
   <deny users="?"/>
  </authorization>

。。。

</system.web>
 <location path="Register.aspx" allowOverride="true">
  <system.web>
   <authorization>
    <allow users="?"/>
   </authorization>
  </system.web>
 </location>

 

Login.aspx.cs

 

 protected void LoginButton_Click(object sender, EventArgs e)
        {
            string userName = this.UserLogin.UserName;
              string Pwd = this.UserLogin.Password;
              if (userName == "李朴" && Pwd == "123")
              {
                  FormsAuthentication.RedirectFromLoginPage(userName, this.UserLogin.RememberMeSet);
                  HttpCookie cookie = FormsAuthentication.GetAuthCookie(userName, this.UserLogin.RememberMeSet);
                  FormsAuthenticationTicket FormAt = FormsAuthentication.Decrypt(cookie.Value);

                  //创建自定义数据
                  String UserData = "id=88|name=李朴|time=" + System.DateTime.Now.Second.ToString();
                  FormsAuthenticationTicket NewFormAt = new FormsAuthenticationTicket(FormAt.Version, FormAt.Name, FormAt.IssueDate, FormAt.Expiration, FormAt.IsPersistent, UserData, FormAt.CookiePath);
                  String EncryptedValue = FormsAuthentication.Encrypt(NewFormAt);
                  cookie.Value = EncryptedValue;
                  Response.Cookies.Add(cookie);
                  Response.Redirect(FormsAuthentication.GetRedirectUrl(userName, this.UserLogin.RememberMeSet), false);
              }
              else
              {
                  Info.Text = "用户名和密码错误,请重新输入。";
              }
         
        }

 

 

Main.aspx.cs

 

 

protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.Name != "")
            {
                this.hplLogin.Text = HttpContext.Current.User.Identity.Name;
                this.hplLogin.NavigateUrl = "";
                 //获得票据里的值:
                FormsIdentity id = (FormsIdentity)User.Identity;
                FormsAuthenticationTicket Ticket = id.Ticket;
                this.Label1.Text = Ticket.UserData;
               
             

            }
        }

 

 

Register.aspx.cs :

 protected void RegBtn_Click(object sender, EventArgs e)
        {
            FormsAuthentication.RedirectFromLoginPage(this.UserNameTxt.Text, true);
             Response.Redirect(FormsAuthentication.GetRedirectUrl(this.UserNameTxt.Text, true), false);
        } 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
+ 订阅

云安全开发者的大本营

其他文章