.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);
        } 

相关文章
|
10月前
|
开发框架 安全 .NET
[牛腩]如何关闭.net framework4.0的请求验证
[牛腩]如何关闭.net framework4.0的请求验证
72 0
|
小程序 安全 API
.NET企业微信回调配置(数据回调URL和指令回调URL验证)(一)
.NET企业微信回调配置(数据回调URL和指令回调URL验证)
802 0
.NET企业微信回调配置(数据回调URL和指令回调URL验证)(一)
|
开发框架 JSON 前端开发
ASP.NET MVC5----常见的数据注解和验证
ASP.NET MVC5----常见的数据注解和验证
300 0
ASP.NET MVC5----常见的数据注解和验证
|
安全
.NET 6新新东西--nuget包验证
.NET 6新新东西--nuget包验证
172 0
|
API C# Windows
分析现有 WPF / Windows Forms 程序能否顺利迁移到 .NET Core 3.0(使用 .NET Core 3.0 Desktop API Analyzer )
原文:分析现有 WPF / Windows Forms 程序能否顺利迁移到 .NET Core 3.0(使用 .NET Core 3.0 Desktop API Analyzer ) 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
1095 0
|
缓存 JSON 安全
从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之五 || Swagger的使用 3.3 JWT权限验证,两种方法
群友反馈: 群里有小伙伴反馈,在Swagger使用的时候报错,无法看到列表,这里我说下如何调试和主要问题: 1、如果遇到问题,这样的:   请在浏览器 =》 F12 ==》 console 控制台 ==》点击错误信息地址 或者直接链接http://localhost:xxxxx/swagger/v1/swagger.
3646 0