使用asp.net2.0的Membership服务与自己写的用户数据库关联

使用asp.net2.0的Membership服务与自己写的用户数据库关联

Membership服务提供了成员服务,往往自己也会写很多的数据库,其中需要和成员关联,完全用Profile不现实,这里就涉及到UserID的问题

我采取的方法是单独建立一个表,建立MemberShip提供的UserID和自己的数据库之间的一对一关系
可能你觉得维护这个表是个很麻烦的事情,其实维护这个表并不麻烦,我们要做的就是在新建和删除用户的时候自动在这张表里加入个删除相应的数据。
我在这个表里面加入了三个字段,UserID存放Guid类型的MemberShip中的ID,uid存放自动增加的uid,为方便起见,也加入了userName字段存放用户名,事实上完全还可以在这个数据表里面加入其他用户信息,不过数据库概论告诉我们,这个一对一的表里面不该放太多的东西

我们新建一个类,继承默认的SqlMembershipProvider
代码如下:

None.gif     public   class  UserMemberShipProvider : SqlMembershipProvider
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
//
InBlock.gif        
// 摘要:
InBlock.gif        
//     向 SQL Server 成员资格数据库添加一个新用户。
InBlock.gif        
//
InBlock.gif        
// 参数:
InBlock.gif        
//   isApproved:
InBlock.gif        
//     是否允许验证新用户。
InBlock.gif        
//
InBlock.gif        
//   passwordAnswer:
InBlock.gif        
//     新用户的密码提示问题答案。
InBlock.gif        
//
InBlock.gif        
//   username:
InBlock.gif        
//     新用户的用户名。
InBlock.gif        
//
InBlock.gif        
//   providerUserKey:
InBlock.gif        
//     唯一标识 SQL Server 数据库中成员资格用户的 System.Guid。
InBlock.gif        
//
InBlock.gif        
//   password:
InBlock.gif        
//     新用户的密码。
InBlock.gif        
//
InBlock.gif        
//   passwordQuestion:
InBlock.gif        
//     新用户的密码提示问题。
InBlock.gif        
//
InBlock.gif        
//   email:
InBlock.gif        
//     新用户的电子邮件地址。
InBlock.gif        
//
InBlock.gif        
//   status:
InBlock.gif        
//     一个 System.Web.Security.MembershipCreateStatus 值,指示是否成功创建用户。
InBlock.gif        
//
InBlock.gif        
// 返回结果:
InBlock.gif        
//     用于新创建用户的 System.Web.Security.MembershipUser 对象。如果没有创建用户,此方法将返回null。
InBlock.gif
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MembershipUser user
=base.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey,out status);
InBlock.gif            
if (status == MembershipCreateStatus.Success)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//添加User 表中内容
InBlock.gif
                DbHelper.ExecuteNonQuery(
InBlock.gif                    
"INSERT INTO [Users] ([userID],[userName]) VALUES (@userID,@userName)",
InBlock.gif                    DbHelper.MakeParameter(
"userID",DbType.Guid,user.ProviderUserKey),
InBlock.gif                    DbHelper.MakeParameter(
"userName",DbType.String,user.UserName)
InBlock.gif                    );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return user;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
//
InBlock.gif        
// 摘要:
InBlock.gif        
//     从 SQL Server 成员资格数据库删除用户的成员资格信息。
InBlock.gif        
//
InBlock.gif        
// 参数:
InBlock.gif        
//   username:
InBlock.gif        
//     要删除的用户的名称。
InBlock.gif        
//
InBlock.gif        
//   deleteAllRelatedData:
InBlock.gif        
//     如果为 true,则从数据库中删除与该用户相关的数据;如果为 false,则将与该用户相关的数据保留在数据库。
InBlock.gif        
//
InBlock.gif        
// 返回结果:
InBlock.gif        
//     如果用户已删除,则为 true;否则为 false。如果数据库中没有此用户,也会返回 false 值。
InBlock.gif
        public override bool DeleteUser(string username, bool deleteAllRelatedData)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object userID = GetUser(username, false).ProviderUserKey;
InBlock.gif            
InBlock.gif            
bool isDeleted = base.DeleteUser(username, deleteAllRelatedData);
InBlock.gif            
if (isDeleted)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//删除User表中内容
InBlock.gif
                DbHelper.ExecuteNonQuery(
InBlock.gif                    
"DELETE FROM [Users] WHERE userID=@userID",
InBlock.gif                    DbHelper.MakeParameter(
"userID", DbType.Guid, userID)
InBlock.gif                );                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return isDeleted;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif

 


其中DbHelper是操作数据库的DAL类,就是做一些基本的添加删除工作

这样在自己的数据库中就可以完全按照uid进行处理,不用管任何和那个Guid相关的问题了。


对了 看看web.config里面怎么写的

None.gif      < membership >
None.gif      
< providers >
None.gif        
< remove  name ="AspNetSqlMembershipProvider" />
None.gif        
< add  name ="AspNetSqlMembershipProvider"
None.gif             type
="ABSC.MemberShipProvider.UserMemberShipProvider"
None.gif             connectionStringName
="LocalSqlServer"
None.gif             enablePasswordRetrieval
="false"
None.gif             enablePasswordReset
="true"
None.gif             requiresQuestionAndAnswer
="true"
None.gif             applicationName
="/"
None.gif             requiresUniqueEmail
="false"
None.gif             passwordFormat
="Hashed"
None.gif             maxInvalidPasswordAttempts
="5"
None.gif             minRequiredPasswordLength
="1"
None.gif             minRequiredNonalphanumericCharacters
="0"
None.gif             passwordAttemptWindow
="10"
None.gif             passwordStrengthRegularExpression
=""
None.gif             
/>
None.gif      
</ providers >
None.gif    
</ membership >

转载于:https://www.cnblogs.com/yayx/archive/2007/06/06/773744.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值