Asp.net关于用户验证小记
由于Sharepoint中使用了Form认证,所以对应的用户管理模块也需要进行编写,今天稍微写了一个。这里对遇到的一些问题和知识点进行记录
1、使用代码进行登录
if (Membership.ValidateUser(Login1.UserName, Login1.Password))
{
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false);
} else {
Login1.FailureText = "用户名或密码错误";
}
2、在web.config中记录用户名密码的两种方法
①记录在appsetting中
Web.config文件中
<appSettings>
<add key="userName" value="fanweiming"/>
<add key="userPwd" value="ymslx-2008"/>
</appSettings>
代码中:
string userName = System.Configuration.ConfigurationSettings.AppSettings["userName"];
string userPwd = System.Configuration.ConfigurationSettings.AppSettings["userPwd"];
if (Login1.UserName == userName && Login1.Password == userPwd)
{
FormsAuthentication.RedirectFromLoginPage(userName, false);
}
②记录在anthem节点中
Web.config文件中
<authentication mode="Forms">
<forms defaultUrl="userAdd.aspx" loginUrl="login.aspx">
<credentials passwordFormat="Clear">
<user name ="fanweiming" password ="fanweiming"/>
<user name ="zhaohui" password ="zhaohui"/>
</credentials>
</forms>
</authentication>
代码中:
bool result = FormsAuthentication.Authenticate(Login1.UserName, Login1.Password);
if (result)
{
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false);
}
3、web.config文件的allow 和deny节点
拒绝所有用户
<sys.web>
<authentication mode="Forms" />
<authorization>
<deny user="*" />
</authorization>
</sys.web>
*(星号)表示所有
?(问号)表示匿名用户
<deny users="?" />
表示不允许未验证用户访问