简体   繁体   English

在 Umbraco 中以编程方式创建新成员

[英]Creating a new member programmatically in Umbraco

I'm trying to create a new member for my Umbraco site programmatically but I'm not confident that I am doing this correctly.我正在尝试以编程方式为我的 Umbraco 站点创建一个新成员,但我不确定我是否正确执行此操作。

My code looks like this:我的代码如下所示:

 MemberType demoMemberType = new MemberType(1040); //id of membertype ‘demo’
 Member newMember = Member.MakeNew(newEmployee.FirstName + " " + newEmployee.LastName, demoMemberType, new umbraco.BusinessLogic.User(0));

 newMember.Email = "test@testmail.com";
 newMember.Password = "password";
 newMember.LoginName = "Test";
 newMember.getProperty("firstName").Value = "test";

 newMember.Save();

But when I run my code, I can't see anything appearing in my Umbraco.但是当我运行我的代码时,我看不到任何东西出现在我的 Umbraco 中。 Can someone please tell me what I've done wrong?有人可以告诉我我做错了什么吗?

If you are using umbraco 7 it is best to user the member service.如果您使用的是 umbraco 7,最好使用会员服务。 Below is a simple approach you could employ to achieve this.以下是您可以用来实现这一目标的简单方法。

Please note ApplicationContext no longer exists for new umbraco versions think from 8+ So just ommit that.请注意ApplicationContext不再存在于 8+ 的新 umbraco 版本中,因此请忽略它。 Everything else works fine.其他一切正常。

public int RegisterMember(string memberName, string emailAddress, string memberPassword, string memberTypeAlias, string memberGroupName)
{
        int umbracoMemberId = -1;

        if (!MemberExists(emailAddress))
        {
            IMember newMember = ApplicationContext.Current.Services.MemberService.CreateMember(emailAddress, emailAddress, memberName, memberTypeAlias);

            try
            {
                ApplicationContext.Current.Services.MemberService.Save(newMember);
                ApplicationContext.Current.Services.MemberService.SavePassword(newMember, memberPassword);
                ApplicationContext.Current.Services.MemberService.AssignRole(newMember.Id, memberGroupName);
                umbracoMemberId = newMember.Id;
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to create new member " + ex.Message);
            }
        }

        return umbracoMemberId;
}


public bool MemberExists(string emailAddress)
{
        return (ApplicationContext.Current.Services.MemberService.GetByEmail(emailAddress) != null);
}

For a more authentic v8 approach you will want to discard ApplicationContext completely and instead use the dependency injection engine as below.对于更真实的 v8 方法,您将希望完全丢弃ApplicationContext ,而是使用如下所示的依赖项注入引擎。 This example assumes you are not able to inject the dependency in this case IMemeberService through the constructor so should cover most cases for most people at least from what I have come across so far especially for those developing applications with umbraco这个例子假设你不能在这种情况下通过构造函数注入依赖项IMemeberService所以应该涵盖大多数人的大多数情况,至少从我目前遇到的情况来看,特别是对于那些使用 umbraco 开发应用程序的人

public int RegisterMember(string memberName, string emailAddress, string memberPassword, string memberTypeAlias, string memberGroupName)
{
        int umbracoMemberId = -1;

        if (!MemberExists(emailAddress))
        {
            try
            {

                IMemberService _memberService = Current.Factory.GetInstance<IMemberService>();

                IMember newMember = _memberService.CreateMember(body.Email, body.Email, celebName, "Member");

                _memberService.Save(newMember);
                _memberService.SavePassword(newMember, body.Password);
                _memberService.AssignRole(newMember.Id, CMSContentConstants.RootNodes.gGourpAliasCelebrityMember);

                umbracoMemberId = newMember.Id;
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to create new member " + ex.Message);
            }
        }

        return umbracoMemberId;
}

The subject is quite involved but the following is some code for your model and controller which should put you on the right track.该主题非常复杂,但以下是您的模型和控制器的一些代码,它们应该能让您走上正轨。 Hopefully you know enough about MVC to get this going.希望您对 MVC 有足够的了解以实现这一目标。

Your model could contain something like the following and be populated with the input from your view您的模型可能包含以下内容,并使用您视图中的输入进行填充

    using System.ComponentModel.DataAnnotations;
    using System.Web;

    namespace MyProject.Models
    {
        public class MemberModel 
        {

            [Required]
            public string Name { get; set; }

            [Required]
            [EmailAddress]
            public string Email { get; set; }

            [Required]
            public string Password { get; set; }
        }
}

Your controller could be something like the following:您的控制器可能如下所示:

using System.Web.Mvc;
using MyProject.Models;
using Umbraco.Web.Mvc;

namespace MyProject.Controllers
{
    public class MemberController : SurfaceController
    {
        public ActionResult SignUp(MemberModel model)
        {
            if (!ModelState.IsValid) 
                return CurrentUmbracoPage();

            var memberService = Services.MemberService;
            if (memberService.GetByEmail(model.Email) != null)
            {
                ModelState.AddModelError("", "Member already exists");
                return CurrentUmbracoPage();
            }
            var member = memberService.CreateMemberWithIdentity(model.Email, model.Email, model.Name, "MyMemberType");

            memberService.Save(member);

            memberService.SavePassword(member,model.Password);

            Members.Login(model.Email, model.Password);

            return Redirect("/");
        }
    }
}

Depending on the Umbraco version the code-sample provided is out of date.根据 Umbraco 版本,提供的代码示例已过时。

Umbraco 4 changed the Umbraco membership model to use the ASP.NET Membership Provider model, meaning that all the abstract classes which are provided Out-Of-The-Box with ASP.NET are capable of accessing the Umbraco Member. Umbraco 4 将 Umbraco 成员资格模型更改为使用 ASP.NET Membership Provider 模型,这意味着所有随 ASP.NET 开箱即用提供的抽象类都能够访问 Umbraco 成员。 There's plenty of good resources on MSDN (and other sites) for using ASP.NET Membership, a suggested starting point is here. MSDN(和其他站点)上有很多关于使用 ASP.NET Membership 的好资源,建议的起点是这里。

Articles:文章:

http://our.umbraco.org/wiki/how-tos/membership-providers http://our.umbraco.org/wiki/how-tos/membership-providers

http://msdn.microsoft.com/en-us/library/tw292whz.aspx http://msdn.microsoft.com/en-us/library/tw292whz.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM