简体   繁体   English

ASP.NET MVC 自定义成员资格提供程序 - 如何重载 CreateUser?

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

I'm trying to build Custom Membership Provider.我正在尝试构建自定义成员资格提供程序。 I would like to capture additional information (First Name and Last Name) during the registration and I'm not using usernames (email is a login).我想在注册期间获取其他信息(名字和姓氏),并且我没有使用用户名(电子邮件是登录名)。

In my custom Membership Provider I'm trying to overload CreateUser method like this:在我的自定义会员提供程序中,我试图重载 CreateUser 方法,如下所示:

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

But when I want to call it from the Account controller:但是当我想从账户 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);
    }

I get the error stating that "No overload for method takes 4 arguments".我收到错误消息,指出“方法没有重载需要 4 个参数”。

How do I call my custom CreateUser method from Custom Membership Provider class?如何从自定义成员资格提供程序 class 调用我的自定义 CreateUser 方法?

Thank you!谢谢!

Isn't it obvious?这不是很明显吗? You're referencing Membership, not MyMembershipProvider when you call Membership.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. 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. Since you can't inherit from static classes, you would have to either call into Membership.XXX from your static version, or implement the functionality yourself.由于您不能从 static 类继承,您必须从 static 版本调用 Membership.XXX,或者自己实现该功能。

Your own MyMembership class could then have any methods you wanted it to have.然后,您自己的 MyMembership class 可以拥有您想要的任何方法。

Another option would be to do something like this:另一种选择是做这样的事情:

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

I don't like this approach though because it requires you to remember to call the provider CreateUser rather than the Membership.CreateUser.我不喜欢这种方法,因为它要求您记住调用提供者 CreateUser 而不是 Membership.CreateUser。 Plus, casting is often a code smell (unless encapsulated, which is why i recommended your own MyMembership static class).另外,铸造通常是一种代码气味(除非封装,这就是为什么我推荐你自己的 MyMembership static 类)。

The problem here is the Membership provider class doesn't give you a first and last name.这里的问题是会员提供者 class 没有给你名字和姓氏。 You could store these in the profile however.但是,您可以将这些存储在配置文件中。 In that case you need to go into AccountController.cs class and change 1. the interface在这种情况下,您需要将 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);
    }

and add a new CreateUser method signature with the fields you want.并使用所需的字段添加新的 CreateUser 方法签名。

Then you will need to implement this method in the AccountController class in that file.然后您需要在该文件的 AccountController class 中实现此方法。

The article you mention below says:您在下面提到的文章说:

"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. "

So - you cannot call the built in CreateUser as they mention Try casting the call in your new CreateUser method to be所以 - 你不能像他们提到的那样调用内置的 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.

相关问题 调试ASP.NET MVC3自定义成员资格提供程序 - Debugging ASP.NET MVC3 custom membership provider ASP.NET MVC3自定义成员资格提供程序 - 指定的成员资格提供程序名称无效 - ASP.NET MVC3 Custom Membership Provider - The membership provider name specified is invalid ASP.NET MVC成员资格提供程序 - ASP.NET MVC Membership provider 如何使用Autofac注入asp.net mvc3自定义成员资格提供程序? - How can you inject an asp.net mvc3 custom membership provider using Autofac? 如何在ASP.NET MVC应用程序中附加自定义成员资格提供程序? - How can I attach a custom membership provider in my ASP.NET MVC application? 如何为ASP.NET MVC 2创建自定义成员资格提供程序? - How do I create a custom membership provider for ASP.NET MVC 2? 如何在ClassLibrary中的ASP.NET MVC 4中重写成员资格提供程序? - How to override the membership provider in ASP.NET MVC 4 in ClassLibrary? ASP.NET 4自定义成员资格提供程序表 - ASP.NET 4 Custom Membership Provider Tables 具有数据提供程序接口问题的ASP.NET MVC2自定义成员资格提供程序 - ASP.NET MVC2 Custom Membership Provider w/ Data Provider Interface Issue ASP.NET MVC 3 单元测试 Membership.CreateUser 总是返回 MembershipCreateStatus 错误 - ASP.NET MVC 3 unit tests for Membership.CreateUser always return MembershipCreateStatus error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM