博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net form验证
阅读量:4204 次
发布时间:2019-05-26

本文共 5407 字,大约阅读时间需要 18 分钟。

web.config (只有用户信息存放在web.config中,才可以使用FormsAuthentication.Authenticate)
login.aspx
<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>    Forms Authentication Login    

Login Page

Username:
Password:
Check here if this is not
a public computer:
login.aspx.cs
protected void Login_Click(object sender, EventArgs e)    {        string username = UserNameTextBox.Text;        string password = UserPassTextBox.Text;        bool isPersistent = PersistCheckBox.Checked;        if (FormsAuthentication.Authenticate(username, password))        {            FormsAuthentication.RedirectFromLoginPage(username, isPersistent);/*            HttpCookie cookie = FormsAuthentication.GetAuthCookie(username, isPersistent);            cookie.Expires = DateTime.Now.AddDays(7);            Response.Cookies.Add(cookie);            Response.Redirect(FormsAuthentication.GetRedirectUrl(username, isPersistent);*/        }        else            throw new Exception("登录失败!!!");    }
default.aspx.cs
protected void Logout_Click(object sender, EventArgs e)    {        FormsAuthentication.SignOut();        FormsAuthentication.RedirectToLoginPage();    }
数据库验证
protected void Login_Click(object sender, EventArgs e)    {        string username = UserNameTextBox.Text;        string password = UserPassTextBox.Text;        bool isPersistent = PersistCheckBox.Checked;        string source = "server=(local);integrated security=SSPI;database=mytest";        string select = "SELECT count(*) FROM [Login] WHERE UserName='" + username + "' AND UserPassword='" + password + "'";        // string update = "UPDATE [Login] set LoginTime=LoginTime+1, LastLogin='" + DateTime.Now + "' WHERE UserName='" + username + "'";        SqlConnection conn = new SqlConnection(source);        conn.Open();        SqlCommand cmd = new SqlCommand(select, conn);        int count = Convert.ToInt32(cmd.ExecuteScalar());        if (count >= 1)        {            // cmd = new SqlCommand(update, conn);            // cmd.ExecuteNonQuery();            string userData = "ApplicationSpecific data for this user.";            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(1), isPersistent, userData, FormsAuthentication.FormsCookiePath);            string encTicket = FormsAuthentication.Encrypt(ticket);            Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));            // if (HttpContext.Current.User.IsInRole("Admin"))                // ......            Response.Redirect(FormsAuthentication.GetRedirectUrl(username, isPersistent));            // 不要使用FormsAuthentication.RedirectFromLoginPage,这个方法会重写cookie。        }        else            throw new Exception("登录失败!!!");    }
Login表
CREATE TABLE [Login] (LoginId smallint IDENTITY(1,1) NOT NULL,UserName nvarchar(20) NOT NULL,UserPassword nvarchar(20) NOT NULL)INSERT INTO [Login] VALUES ('xiaobai', 'xiaobai')
角色验证
protected void Login_Click(object sender, EventArgs e)    {        string username = UserNameTextBox.Text;        string password = UserPassTextBox.Text;        bool isPersistent = PersistCheckBox.Checked;        string source = "server=(local);integrated security=SSPI;database=mytest";        string select = "SELECT count(*) FROM [Login] WHERE UserName='" + username + "' AND UserPassword='" + password + "'";        SqlConnection conn = new SqlConnection(source);        conn.Open();        SqlCommand cmd = new SqlCommand(select, conn);        int count = Convert.ToInt32(cmd.ExecuteScalar());        if (count >= 1)        {            string userData = "Admin";            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(1), isPersistent, userData, FormsAuthentication.FormsCookiePath);            string encTicket = FormsAuthentication.Encrypt(ticket);            Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));            Response.Redirect(FormsAuthentication.GetRedirectUrl(username, isPersistent));        }        else            throw new Exception("登录失败!!!");    }
Global.asax
<%@ Import Namespace="System.Security.Principal" %>protected void Application_AuthenticateRequest(object sender, EventArgs e)    {        HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];        if (null == authCookie)            return;        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);        string[] roles = authTicket.UserData.Split(new char[] { ',' });        // Context.User = new GenericPrincipal(new FormsIdentity(authTicket), roles);        Context.User = new GenericPrincipal(Context.User.Identity, roles);    }

转载地址:http://apsli.baihongyu.com/

你可能感兴趣的文章
小米笔试:循环排序数组二分查找
查看>>
POJ 2250解题报告
查看>>
POJ 3239解题报告
查看>>
POJ 1915解题报告
查看>>
POJ 2488解题报告
查看>>
POJ 2243解题报告
查看>>
POJ 3126解题报告
查看>>
POJ 2248解题报告
查看>>
POJ 1169解题报告
查看>>
POJ 1166解题报告
查看>>
POJ 3006解题报告
查看>>
POJ 1163解题报告
查看>>
POJ 2954解题报告
查看>>
POJ 1590解题报告
查看>>
无法设置未定义或 null 引用的属性“innerHTML”
查看>>
如何使button自动填充table的单元格
查看>>
input标签下的常用功能
查看>>
HTML获取当前时间年月日时分秒等相关信息
查看>>
在IDEA中安装阿里校验代码规范的插件的方式和可能会用遇见的问题及解决方法
查看>>
规范的Java项目命名格式
查看>>