html表单 asp验证,Asp.net Form表单验证(登录验证)

一: ASP.NET 的安全认证模式

Windows, Forms, Passport, None

二: 修改验证模式

修改Web.config

三: 用户登陆,发身份验证票

FormsAuthentication.FormsCookieName ~ 就是上面的HotelUser

//登陆成功后,返回请求页面

a:System.Web.Security.FormsAuthentication.RedirectFromLoginPage(FormsAuthentication.FormsCookieName,false);

//发验证票,到指定页面

b:System.Web.Security.FormsAuthentication.SetAuthCookie(FormsAuthentication.FormsCookieName,false);

Response.Redirect("Default.aspx");

四: 用户退出

System.Web.Security.FormsAuthentication.SignOut();

五: 用户是否通过验证

User.Identity.IsAuthenticated //如果通过验证,或是有Cookie存在

值为True,否则为False

六: Web.config 作用范围

0: machine.config 的设置

作用整个机器所有目录及其目录下所有文件. -->子随父姓

1: Web.config 的设置

作用于所在目录的所有文件及其子目录下所有文. -->子随父姓

2: 子目录下的 Web.config

设置将覆盖由父目录继承下来的设置 -->将在外,军命有所不受

七: 设置某个文件夹或文件的访问权限

1:在相应文件夹下 建立web.config文件

//这里设置访问权限

2: 在根目录web.config下设置整个站点 所有文件夹 和 文夹的访问权限

//目录文件夹1

//

为某个文件设置访问配置

//目录文件夹2

//原根目录web.config 配置

八:单点登陆

1:获取机器key 生成密钥

protected

void btn_OK_Click(object sender, EventArgs e)

{

string decStr =

this.CreateKeyString(int.Parse(this.TextBox1.Text));

string valStr =

this.CreateKeyString(int.Parse(this.TextBox2.Text));

this.TextBox3.Text = string.Format("", valStr, decStr);

}

///

/// 生成加密型强随机

Key 值

///

/// Key

的有效长度:

///

decryptionKey 的有效值为 8 或 24;

///

validationKay 的有效值为 20 至 64

///

private

string CreateKeyString(int i)

{

System.Security.Cryptography.RNGCryptoServiceProviderrng = new

System.Security.Cryptography.RNGCryptoServiceProvider();

//加密随机数生成器

byte[] bt = new byte[i];

rng.GetBytes(bt);//用加密型强随机值序列填充字节数组

System.Text.StringBuilder str = new

System.Text.StringBuilder();

for (int j = 0; j < i; j++)

{

str.Append(string.Format("{0:X2}", bt[j])); //转换成大写的十六进制文本

}

return str.ToString();

}

2:在要实现单点登陆的项目根web.config中添加密钥;

a) 两个项目Web.cinfig的 节点确保以下几个字段完全一样:validationKey 、decryptionKey

、validation

b) 两个项目的 Cookie 名称必须相同,也就是 中的 name 属性,这里我们把它统一为 name

="UserLogin" //name="www.wf.com"

c) 注意区分大小写

d) 登陆页面整合到统一登陆点登陆 比如:loginUrl="www.wf.com/login.aspx",

在登陆页面发放验证票

//项目网站1

//项目网站2

3: 给用户发放Cookie

<1>: 一次登陆后,给各个站点同时发放Cookie认证。

<2>: 一次登陆后,根据用户选择性的发放Cookie认证。

九: Cookie

1: 普通Cookie

protected void

Button1_Click(object sender, EventArgs e)

{

HttpCookie ck = new HttpCookie("str");

ck.Expires.AddDays(1);

ck["rr"] = "str_rr_";

ck["w1"] =

"str_w1_"; Response.Cookies.Add(ck);

HttpCookie ckNex = new HttpCookie("Nex");

ck.Expires.AddDays(1); ck.value=

"Nex_"; Response.Cookies.Add(ckNex); }

protected

void Button2_Click(object sender, EventArgs e)

{ TextBox1.Text = Request.Cookies["str"]["w1"].ToString() +

Request.Cookies["str"]["rr"].ToString(); }

2:

生成用户验证的Cookie public void AuthenticationUsers(string userName)

{

FormsAuthenticationTicket tichet = new FormsAuthenticationTicket(1,

userName, DateTime.Now, DateTime.Now.AddHours(24), true, "");

string hashTichet = FormsAuthentication.Encrypt(tichet);

HttpCookie userCookie = new

HttpCookie(FormsAuthentication.FormsCookieName);

userCookie.Value = hashTichet;

userCookie.Expires = tichet.Expiration;

userCookie.Domain = FormsAuthentication.CookieDomain;

HttpContext.Current.Response.Cookies.Add(userCookie);

}

这个Cookie 相当与 下面两发放的Cookie

//登陆成功后,返回请求页面

a:System.Web.Security.FormsAuthentication.RedirectFromLoginPage(FormsAuthentication.FormsCookieName,false);

//发验证票,到指定页面

b:System.Web.Security.FormsAuthentication.SetAuthCookie(FormsAuthentication.FormsCookieName,false);

泸州社区

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值