成员管理
一、 Membership 类 : 创建和删除用户, 检索用户信 ,生成随机密码 , 登录验证
创建新用户:
try
{
Membership.CreateUser ("name", "password", "mail");
}
catch
(MembershipCreateUserException e)
{
// 失败
switch (e.StatusCode)
{
case MembershipCreateStatus.DuplicateUsername:
case MembershipCreateStatus.DuplicateEmail:
case MembershipCreateStatus.InvalidPassword:
default:
}
}
登录验证:
if
(Membership.ValidateUser (UserName.Text, Password.Text))
FormsAuthentication.RedirectFromLoginPage (UserName.Text,
RememberMe.Checked);
方法:CreateUser(创建用户) DeleteUser(删除用户) GeneratePassword(生产随即密码) GetAllUsers(得到用户) GetUser(查看某个用户) UpdateUser(修改用户) ValidateUser(验证是否成功)
二、 MembershipUser类:
描述在成员数据存储中单一的注册用户信息
包含了众多的属性来获取和设置用户信息
包含方法来检索、改变和重设密码
通过诸如GetUser 和CreateUser的属性返回值
属性:Comment CreationDate Email LastLoginDate LastPasswordChangedDate UserId UserName
方法: ChangePassword ChangePassword-QuestionAndAnswer GetPassword ResetPassword
挂起登录权限:
if
(Membership.ValidateUser (UserName.Text, Password.Text))
{
MembershipUser user = Membership.GetUser (UserName.Text);
user.Comment = "0"; //记录登录次数
RedirectFromLoginPage (UserName.Text, RememberMe.Checked);
}
else
{
MembershipUser user = Membership.GetUser (UserName.Text);
if (user != null) {
string count = Convert.ToInt32 (user.Comment) + 1;
user.Comment = count.ToString ();
}
}
三 使用 SQL Server提供程序
<
configuration
>
<
system
.web
>
<
membership
defaultProvider
="AspNetSqlProvider"
/>
</
system.web
>
</
configuration
>
更改提供程序配置:
<
membership
>
<
providers
>
<
remove
name
="AspNetSqlProvider"
/>
<
add
name
="AspNetSqlProvider"
type
="System.Web.Security.SqlMembershipProvider, System.Web, "
connectionStringName
="RemoteSqlServer"
enablePasswordRetrieval
="false"
enablePasswordReset
="true"
requiresQuestionAndAnswer
="false"
applicationName
="/"
requiresUniqueEmail
="false"
passwordFormat
="Hashed"
description
="Stores and retrieves membership data "
/>
</
providers
>
</
membership
>
配置提供程序
成员提供程序支持许多配置选项,密码如何被存储 (明文, 散列, 加密)?密码是否允许被恢复?用户是否必须有一个唯一的e-mail地址?通过提供程序类属性来表现,在配置文件中进行初始化
四、角色管理
方法:AddUserToRole CreateRole DeleteRole GetRolesForUser(查看用户角色) GetUsersInRole IsUserInRole RemoveUserFromRole
创建新角色
if
(
!
Roles.RoleExists (
"
Developers
"
))
{
Roles.CreateRole ("Developers");
}
增加用户到一个角色
string
name
=
Membership.GetUser ().Username;
Roles.AddUserToRole (name,
"
Developers
"
);
配置Web.config启用角色
<
configuration
>
<
system
.web
>
<
roleManager
enabled
="true"
/>
</
system.web
>
</
configuration
>
启用角色高速缓存
<
configuration
>
<
system
.web
>
<
roleManager
enabled
="true"
cacheRolesInCookie
="true"
/>
<!--
Other roleManager attributes (and their defaults) include:
cookieName=".ASPXROLES" // Cookie name
cookieTimeout="30" // Cookie lifetime
cookiePath="/" // Cookie path
cookieRequireSSL="false" // Restrict cookie to SSL?
cookieSlidingExpiration="true" // Renew expiring cookies?
createPersistentCookie="false" // Issue persistent cookie?
cookieProtection="All" /> // Cookie protection level
-->
</
system.web
>
</
configuration
>
使用SQL Server提供程序
<
configuration
>
<
system
.web
>
<
roleManager
enabled
="true"
defaultProvider
="AspNetSqlRoleProvider"
/>
</
system.web
>
</
configuration
>
错误提示:
密码最短长度为 7,其中必须包含以下非字母数字字符: 1
解决方案:
收到以上的消息主要是在创建用户的时候产生的,对于用Asp.net 网站管理工具的时候创建用户也会产生。
主要是密码输入不符合要求,要改变上面的规定时,主要有两种方法:
1.所有的站点都改变。
找到machine.config文件
<
membership
>
<
providers
>
<
add
name
="AspNetSqlMembershipProvider"
type
="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName
="LocalSqlServer"
enablePasswordRetrieval
="false"
enablePasswordReset
="true"
requiresQuestionAndAnswer
="true"
applicationName
="/"
requiresUniqueEmail
="false"
passwordFormat
="Hashed"
maxInvalidPasswordAttempts
="5"
minRequiredPasswordLength
="7"
minRequiredNonalphanumericCharacters
="1"
passwordAttemptWindow
="10"
passwordStrengthRegularExpression
=""
/>
</
providers
>
</
membership
>
里面有两个属性,一个是 minRequiredPasswordLength,意思是最长密码,默认为7另一个是minRequiredNonalphanumericCharacters,默认为1,意思是至少有一个非字母字符,只要把它改成0就可以了。
2.假如只是对某一个站点,只要修改web.config的值就ok了
修改如上,把上面的代码插入在
<
system
.web
>
下面就ok了。
如要改成密码规则是"至少6个字符,而不用特殊字符的",如下:
(注:一定要加上
<
remove
name
="AspNetSqlMembershipProvider"
/>
,否则会提示"项“AspNetSqlMembershipProvider”已添加"的出错信息)
<
membership
>
<
providers
>
<
remove
name
="AspNetSqlMembershipProvider"
/>
<
add
name
="AspNetSqlMembershipProvider"
type
="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName
="LocalSqlServer"
enablePasswordRetrieval
="false"
enablePasswordReset
="true"
requiresQuestionAndAnswer
="true"
applicationName
="/"
requiresUniqueEmail
="false"
passwordFormat
="Hashed"
maxInvalidPasswordAttempts
="5"
minRequiredPasswordLength
="6"
minRequiredNonalphanumericCharacters
="0"
passwordAttemptWindow
="10"
passwordStrengthRegularExpression
=""
/>
</
providers
>
</
membership
>