如何在CreateUser方法ASP.Net MVC中添加更多功能

时间:2014-03-03 17:02:50

标签: c# sql asp.net-mvc-4

我在Microsoft Visual Studio 2010 ASP.Net和MVC4中开发了一个网站。我还启用了用户帐户管理功能,默认来自visual studio。当我调用“创建用户”方法时,我发现了以下方法

  membershipService.CreateUser(model.UserName, model.Password, model.Email,
     model.SecretQuestion, model.SecretAnswer, false, out createStatus);

此方法只会在数据库中添加列,以便输入密码,电子邮件等。我想添加用户公司名称,姓氏和名字等等。当我看到类IMembership时,这个类被锁定了我不能做任何改变。

 #region Assembly SecurityGuard.dll, v4.0.30319
 // C:\Users\wwwimc\Desktop\WebFrontEnd_26012014\packages\SecurityGuard.MVC4.1.0.10   
 \lib\net40\SecurityGuard.dll
 #endregion

using System;
using System.Web.Security;

namespace SecurityGuard.Interfaces
{
   public interface IMembershipService
  {
    string ApplicationName { get; set; }
    bool EnablePasswordReset { get; }
    bool EnablePasswordRetrieval { get; }
    string HashAlgorithmType { get; }
    int MaxInvalidPasswordAttempts { get; }
    int MinRequiredNonAlphanumericCharacters { get; }
    int MinRequiredPasswordLength { get; }
    int PasswordAttemptWindow { get; }
    string PasswordStrengthRegularExpression { get; }
    MembershipProvider Provider { get; }
    MembershipProviderCollection Providers { get; }
    bool RequiresQuestionAndAnswer { get; }
    int UserIsOnlineTimeWindow { get; }

    event MembershipValidatePasswordEventHandler ValidatingPassword;

    MembershipUser CreateUser(string username, string password);
    MembershipUser CreateUser(string username, string password, string email);
    MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, out MembershipCreateStatus status);
    MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status);
    bool DeleteUser(string username);
    bool DeleteUser(string username, bool deleteAllRelatedData);
    MembershipUserCollection FindUsersByEmail(string emailToMatch);
    MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords);
    MembershipUserCollection FindUsersByName(string usernameToMatch);
    MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords);
    string GeneratePassword(int length, int numberOfNonAlphanumericCharacters);
    MembershipUserCollection GetAllUsers();
    MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords);
    int GetNumberOfUsersOnline();
    MembershipUser GetUser();
    MembershipUser GetUser(bool userIsOnline);
    MembershipUser GetUser(object providerUserKey);
    MembershipUser GetUser(string username);
    MembershipUser GetUser(object providerUserKey, bool userIsOnline);
    MembershipUser GetUser(string username, bool userIsOnline);
    string GetUserNameByEmail(string emailToMatch);
    void UpdateUser(MembershipUser user);
    bool ValidateUser(string username, string password);
}

}

有人可以建议任何方式进行更改,以便通过新注册,我可以添加有关用户的更多信息吗?

2 个答案:

答案 0 :(得分:1)

添加您自己的类以扩展SecurityGuard的MembershipService

using System.Web.Security;
using SecurityGuard.Services;

namespace MyMembershipNamespace
{
    public class MyMembershipService : MembershipService
    {
        public MyMembershipService(MembershipProvider membershipProvider)
        : base(membershipProvider)
        {
        }

        public MembershipUser CreateUser(string username, string password, string email, string company, string phone)
        {
            //Add code here to save company and phone to the database

            //allow base to execute normally
            return base.CreateUser(username, password, email);
        }
    }
}

答案 1 :(得分:0)

您可以先创建您想要ADD属性的类 然后将该对象设置为

object providerUserKey

public override MembershipUser CreateUser(string userName, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)

获取此对象值并为其设置Linq(添加)。