身份验证(Authentication):是指识别用户的过程
授权(Authorization):是指根据用户身份来授予访问权限
匿名用户:
默认情况下,匿名访问用户被命名为IUSER_machinename。使用这个账户可以控制匿名用户对服务器上资源的访问
验证用户:
ASP.net web应用程序提供3种对用户身份验证和授权的主要方式:
1.集成windows验证(默认):通过域控制器对网络用户进行验证。
2.窗体验证:以表单的方式收集用户输入的用户名和密码。
3.Passport验证:
Windows验证:
<!-- 身份验证
此节设置应用程序的身份验证策略。可能的模式是 "Windows"、
"Forms"、 "Passport" 和 "None"
"None" 不执行身份验证。
"Windows" IIS 根据应用程序的设置执行身份验证
(基本、简要或集成 Windows)。在 IIS 中必须禁用匿名访问。
"Forms" 您为用户提供一个输入凭据的自定义窗体(Web 页),然后
在您的应用程序中验证他们的身份。用户凭据标记存储在 Cookie 中。
"Passport" 身份验证是通过 Microsoft 的集中身份验证服务执行的,
它为成员站点提供单独登录和核心配置文件服务。
-->
<authentication mode="Windows" />
<!-- 授权
此节设置应用程序的授权策略。可以允许或拒绝不同的用户或角色访问
应用程序资源。通配符: "*" 表示任何人,"?" 表示匿名
(未经身份验证的)用户。
-->
<authorization>
<!--<allow users="?"/>-->
<deny users="?" />
<deny users="*" />
<allow roles="Administrators" />
<!-- <allow users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/>
<deny users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/>
-->
</authorization>
此节设置应用程序的身份验证策略。可能的模式是 "Windows"、
"Forms"、 "Passport" 和 "None"
"None" 不执行身份验证。
"Windows" IIS 根据应用程序的设置执行身份验证
(基本、简要或集成 Windows)。在 IIS 中必须禁用匿名访问。
"Forms" 您为用户提供一个输入凭据的自定义窗体(Web 页),然后
在您的应用程序中验证他们的身份。用户凭据标记存储在 Cookie 中。
"Passport" 身份验证是通过 Microsoft 的集中身份验证服务执行的,
它为成员站点提供单独登录和核心配置文件服务。
-->
<authentication mode="Windows" />
<!-- 授权
此节设置应用程序的授权策略。可以允许或拒绝不同的用户或角色访问
应用程序资源。通配符: "*" 表示任何人,"?" 表示匿名
(未经身份验证的)用户。
-->
<authorization>
<!--<allow users="?"/>-->
<deny users="?" />
<deny users="*" />
<allow roles="Administrators" />
<!-- <allow users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/>
<deny users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/>
-->
</authorization>
一但用户登入,就可以用User对象的Identity属性来获取关于用户的信息了。User对象还提供一个IsInRole方法来判断当前用户的角色
private void Page_Load(object sender,System.EventArgs e)
{
lbl_Authenticated.InnerText=Use.Identity.IsAuthenicated.ToString();
lbl_name.InnerText=User.Identity.Name;
lbl_AuthenticationType=User.Identity.AuthenticationType;
if(User.IsInRole("Administrator")
{
}
}
{
lbl_Authenticated.InnerText=Use.Identity.IsAuthenicated.ToString();
lbl_name.InnerText=User.Identity.Name;
lbl_AuthenticationType=User.Identity.AuthenticationType;
if(User.IsInRole("Administrator")
{
}
}
Form的验证:
是可以将用户和密码放在web.config文件中
<authentication mode="Forms">
<forms loginUrl="LogIn.aspx">
<cerdenticals passwordFormat="clear">
<user name="June",password="Junebug" />
</credentitals>
</forms>
</authentication>
<authorization>
<deny user="?" />
<authorization>
<forms loginUrl="LogIn.aspx">
<cerdenticals passwordFormat="clear">
<user name="June",password="Junebug" />
</credentitals>
</forms>
</authentication>
<authorization>
<deny user="?" />
<authorization>
但如果用户的信息是放在db中则<user>的标记可以不用。
不管把信息储存在那里,都可以使用FormsAuthentication类的HashPasswordForStoringInConfigFile方法加密密码,他提供SHA1和MD5算法。
以下两种存放信息方法的简单实现:
1.把用户名和密码保存在web.config中
using System.Web.Security
private void butSingOn_Click(object sender,System.EventArgs e)
{
if(FormsAuthentication.Authenticate(textUserName.Text,textPassword.Text))
FormsAuthentication.RedirectFromLoginPage(textUserName.Text,true);
else
{
txtPassword.Text="";
if(System.Convert.ToInt32(ViewState["Tries"])>2)
Response.Redirect("Denied.htm");
else
ViewState["Tires"]=System.Convert.ToInt32(["Tries"])+1;
}
}
private void butSingout_Click(object sender,System.EventArgs e)
{
FormAuthentication.Singout();
Response.Redirect("UserInfo.aspx");
}
private void butSingOn_Click(object sender,System.EventArgs e)
{
if(FormsAuthentication.Authenticate(textUserName.Text,textPassword.Text))
FormsAuthentication.RedirectFromLoginPage(textUserName.Text,true);
else
{
txtPassword.Text="";
if(System.Convert.ToInt32(ViewState["Tries"])>2)
Response.Redirect("Denied.htm");
else
ViewState["Tires"]=System.Convert.ToInt32(["Tries"])+1;
}
}
private void butSingout_Click(object sender,System.EventArgs e)
{
FormAuthentication.Singout();
Response.Redirect("UserInfo.aspx");
}
2.把用户名和密码保存到db中
//添加用户
private void butNewUser_Click(object sender,System.EventArgs e)
{
if(AddUser(txtUname.text,txtPwd.text)
//处理插入成功
else
//处理插入失败
}
private bool AddUser(string Uname,string Password)
{
Password=FormsAuthentication.HashPasswordForStoringInConfigFile(Password,"SHA1");
//数据库操作,插入成功返回true 否则返回false
}
//验证用户
private void buSingOn_Click(object sender,System.EventArgs e)
{
if(CheckPassword(txtUname.Text,txtPwd.Text)
FormsAuthentication.RedirectFromLoginPage(txtUname.Text,true);
else
{
//处理登入失败
}
}
private bool CheckPassword(string Uname,string Password)
{
Password = FormsAuthentication.HashPasswordForStoringInconfigFile(Password,"SHA1");
//数据库操作,如果用户输入的用户名和密码正确则返回true否则返回false
}
private void butNewUser_Click(object sender,System.EventArgs e)
{
if(AddUser(txtUname.text,txtPwd.text)
//处理插入成功
else
//处理插入失败
}
private bool AddUser(string Uname,string Password)
{
Password=FormsAuthentication.HashPasswordForStoringInConfigFile(Password,"SHA1");
//数据库操作,插入成功返回true 否则返回false
}
//验证用户
private void buSingOn_Click(object sender,System.EventArgs e)
{
if(CheckPassword(txtUname.Text,txtPwd.Text)
FormsAuthentication.RedirectFromLoginPage(txtUname.Text,true);
else
{
//处理登入失败
}
}
private bool CheckPassword(string Uname,string Password)
{
Password = FormsAuthentication.HashPasswordForStoringInconfigFile(Password,"SHA1");
//数据库操作,如果用户输入的用户名和密码正确则返回true否则返回false
}
注意:htm与html文件的身份验证由IIS提供。