簡體   English   中英

ASP.NET MVC 自定義成員資格提供程序 - 如何重載 CreateUser?

[英]ASP.NET MVC Custom Membership Provider - How to overload CreateUser?

我正在嘗試構建自定義成員資格提供程序。 我想在注冊期間獲取其他信息(名字和姓氏),並且我沒有使用用戶名(電子郵件是登錄名)。

在我的自定義會員提供程序中,我試圖重載 CreateUser 方法,如下所示:

    public override MyMembershipUser CreateUser(string firstName, string lastName, 
                                       string email, string password)
    {
        ...
    }

但是當我想從賬戶 controller 調用它時:

    [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            MembershipCreateStatus createStatus;
            Membership.CreateUser(model.FirstName, model.LastName, 
                                  model.Email, model.Password);

            if (createStatus == MembershipCreateStatus.Success)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", ErrorCodeToString(createStatus));
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

我收到錯誤消息,指出“方法沒有重載需要 4 個參數”。

如何從自定義成員資格提供程序 class 調用我的自定義 CreateUser 方法?

謝謝!

這不是很明顯嗎? 當您調用 Membership.CreateUser 時,您引用的是 Membership,而不是 MyMembershipProvider。

It looks like Membership is a static class that internally calls into your membership provider, so there is no real way to change this other than creating your own "MyMembership" static class that does the same thing. 由於您不能從 static 類繼承,您必須從 static 版本調用 Membership.XXX,或者自己實現該功能。

然后,您自己的 MyMembership class 可以擁有您想要的任何方法。

另一種選擇是做這樣的事情:

((MyMembershipProvider)Membership.Provider).CreateUser()

我不喜歡這種方法,因為它要求您記住調用提供者 CreateUser 而不是 Membership.CreateUser。 另外,鑄造通常是一種代碼氣味(除非封裝,這就是為什么我推薦你自己的 MyMembership static 類)。

這里的問題是會員提供者 class 沒有給你名字和姓氏。 但是,您可以將這些存儲在配置文件中。 在這種情況下,您需要將 go 放入 AccountController.cs class 並更改 1. 界面

    public interface IMembershipService
    {
        int MinPasswordLength { get; }

        bool ValidateUser(string userName, string password);
        MembershipCreateStatus CreateUser(string userName, string password, string email);
        bool ChangePassword(string userName, string oldPassword, string newPassword);
    }

並使用所需的字段添加新的 CreateUser 方法簽名。

然后您需要在該文件的 AccountController class 中實現此方法。

您在下面提到的文章說:

"However, this overload will not be called by the Membership class or controls that rely on the Membership class, such as the CreateUserWizard control. To call this method from an application, cast the MembershipProvider instance referenced by the Membership class as your custom membership provider type, and then call your CreateUseroverload directly. "

所以 - 你不能像他們提到的那樣調用內置的 CreateUser 嘗試在你的新 CreateUser 方法中調用

public MembershipCreateStatus CreateUser(string firstName, string lastName, string password, string email)
        {
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");
            if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");
//etc....
            MembershipCreateStatus status;
((YourCustomProvider)_provider).CreateUser(firstName, lastName, password, email, null, null, true, null, out status);            return status;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM