扩展Membership.CreateUser方法的参数

时间:2011-04-17 12:28:01

标签: c# .net asp.net asp.net-membership

我使用此代码调用我的自定义成员资格提供程序方法:

 Membership.CreateUser(newUser.UserName, newUser.Password);

但我需要扩展一些参数。我写这个来覆盖方法:

 public override MembershipUser CreateUser(string username, string password, string email, 
    string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
    try
    {
        int max = 1;
        int RoleID = 0;
        using (var context = new KowsarSiteEntities())
        {
            max += (from i in context.aspnet_Users
                    select i).Max(itm => itm.UserId);
            RoleID = (from v in context.aspnet_Roles
                      where v.RoleName.ToLower().Equals(RoleName)
                      select v.RoleId).FirstOrDefault();
            KowsarSiteModel.aspnet_Membership member =                    
                KowsarSiteModel.aspnet_Membership.Createaspnet_Membership
                    (ApplicationID, max, GetHashedPassword(password), 1);
            KowsarSiteModel.aspnet_Users ASPNETusers = 
                KowsarSiteModel.aspnet_Users.Createaspnet_Users(ApplicationID, max, username);
            KowsarSiteModel.aspnet_UsersInRoles userInRoles =
                KowsarSiteModel.aspnet_UsersInRoles.Createaspnet_UsersInRoles(max, RoleID);
            context.AddToaspnet_Membership(member);
            context.AddToaspnet_Users(ASPNETusers);
            context.AddToaspnet_UsersInRoles(userInRoles);
            context.SaveChanges();
        }
        status = MembershipCreateStatus.Success;
    }
    catch (Exception ex)
    {
        string str = ex.Message;
        status = MembershipCreateStatus.UserRejected;
    }
    return null;
}

我需要为CreateUser方法添加一些参数但是如何成为可能会员资格是一个静态类我如何自定义它?

2 个答案:

答案 0 :(得分:2)

((YourProviderType)Membership.Provider).CreateUser(yourParameters);

答案 1 :(得分:1)

如果您不符合他们的界面,则不能使用System.Web.Security提供的静态类。

正如abatishchev指出的那样,你不必使用静态类;你可以随时使用自己的班级。