csharp 一堆Umbraco Membership API片段
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 一堆Umbraco Membership API片段相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security.
using Umbraco.Core.Services;
using Umbraco.Web.Models;
using Umbraco.Web.PublishedCache;
/*
*
* MembershipHelper
*
*/
// Creates a new register model. Will be constructed with member properties defined on the "MyMemberTypeAlias" within Umbraco.
var newMember = this.Members.CreateRegistrationModel("MyMemberTypeAlias");
/*
* Other ways of creating a RegisterModel. Avoid!
*/
// Nay! Assumes that a default member type for a baked in alias exists!
newMember = this.Members.CreateRegistrationModel();
// Yay for mutability! >_>
/* Nay! Attempts to manually construct an instance of MembershipHelper based off the
* current context and also assumes that a default member type for a baked in alias exists! */
newMember = new RegisterModel();
/* Nay! Creates a completely vanilla instance of the model and also assumes that a default
* member type for a baked in alias exists! */
newMember = RegisterModel.CreateModel();
/*
* Standard properties that are required to be set for the registration to succeed.
*/
newMember.Name = "Mike Bowen";
newMember.Email = "milquetoastable@gmail.com";
newMember.Password = "24DaysInUmbraco";
// UsernameIsEmail needs to be set to false in order for the Username property to be persisted.
newMember.UsernameIsEmail = false;
newMember.Username = "mike";
newMember.LoginOnSuccess = false;
/*
* Setting custom properties defined on the member type. The property must be set as "Member can edit" within the Umbraco interface.
*/
UmbracoProperty favouriteLanguage = newMember.MemberProperties.Single(p => p.Alias == "favouriteLanguage");
favouriteLanguage.Value = "Crystal";
/*
* Saving the member.
*/
MembershipCreateStatus status = new MembershipCreateStatus();
MembershipUser member = this.Members.RegisterMember(newMember, out status);
// Check the status of the registration.
if (status == MembershipCreateStatus.Success)
{
// Add a role ("member group" in Umbraco) to the newly created member.
Roles.AddUserToRole(newMember.Username, "MyMemberGroup");
// Members are automatically approved.
member.IsApproved = false;
// Re-save the member.
Membership.UpdateUser(member);
}
/*
*
* MemberService
*
*/
IMemberService service = ApplicationContext.Current.Services.MemberService;
// Creates a new member object to persist later on.
IMember newMember = service.CreateMember("milquetoastable@gmail.com", "milquetoastable@gmail.com", "Mike Bowen", "MyMemberType");
// Ostensibly the same methods. Creates and immediately persists a new member.
newMember = service.CreateMemberWithIdentity("milquetoastable@gmail.com", "milquetoastable@gmail.com", "Mike Bowen", "MyMemberType");
newMember = service.CreateWithIdentity("milquetoastable@gmail.com", "milquetoastable@gmail.com", "Mike Bowen", "MyMemberType");
// Setting custom properties.
newMember.SetValue("favouriteLanguage", "Ruby");
// Setting standard membership properties. Requires less hoop jumping than using MembershipHelper.
newMember.Comments = "Here's an member";
newMember.IsApproved = false;
newMember.PasswordQuestion = "Awesome?";
newMember.RawPasswordAnswerValue = "AWESOME";
service.Save(newMember);
service.SavePassword(newMember, "24DaysInUmbraco");
// Assigning roles.
service.AssignRoles(new [] { newMember.Id }, new [] { "MyMemberGroup", "MyOtherMemberGroup" });
以上是关于csharp 一堆Umbraco Membership API片段的主要内容,如果未能解决你的问题,请参考以下文章
csharp 使用Umbraco Examine API搜索多个术语。