0

我想在客户端管理用户和角色,但我找不到实现这一点的方法。链接或代码将不胜感激。

4

1 回答 1

0

很抱歉这个被遗弃的问题,我已经在很多天前解决了这个问题,我填写我应该在这里发布答案,因为我无法在网络上找到完整的答案,这可以帮助那里的一些陷入困境的灵魂。除了我现在不记得的链接但是,我向博客所有者道歉,甚至不记得他的名字,但是,这里是完整的历史:

首先创建一个类来包装 MembershipUser 和另一个类来包装 MembsershipRole:

    public class MembershipServiceUser
            {
                public string Comment { get; set; }
                [Editable(false)]
                public DateTime CreationDate { get; set; }
                [Key]
                [Editable(false, AllowInitialValue = true)]
                public string Email { get; set; }
                public bool IsApproved { get; set; }
                [Editable(false)]
                public bool IsLockedOut { get; set; }
                [Editable(false)]
                public bool IsOnline { get; set; }
                public DateTime LastActivityDate { get; set; }
                [Editable(false)]
                public DateTime LastLockoutDate { get; set; }
                public DateTime LastLoginDate { get; set; }
                [Editable(false)]
                public DateTime LastPasswordChangedDate { get; set; }
                [Editable(false)]
                public string PasswordQuestion { get; set; }
                [Key]
                [Editable(false, AllowInitialValue = true)]
                public string UserName { get; set; }

                public MembershipServiceUser() { }
                public MembershipServiceUser(MembershipUser user)
                {
                    this.FromMembershipUser(user);
                }

                public void FromMembershipUser(MembershipUser user)
                {
                    this.Comment = user.Comment;
                    this.CreationDate = user.CreationDate;
                    this.Email = user.Email;
                    this.IsApproved = user.IsApproved;
                    this.IsLockedOut = user.IsLockedOut;
                    this.IsOnline = user.IsOnline;
                    this.LastActivityDate = user.LastActivityDate;
                    this.LastLockoutDate = user.LastLockoutDate;
                    this.LastLoginDate = user.LastLoginDate;
                    this.LastPasswordChangedDate = user.LastPasswordChangedDate;
                    this.PasswordQuestion = user.PasswordQuestion;
                    this.UserName = user.UserName;
                }

                public MembershipUser ToMembershipUser()
                {
                    MembershipUser user = Membership.GetUser(this.UserName);

                    if (user.Comment != this.Comment) user.Comment = this.Comment;
                    if (user.IsApproved != this.IsApproved) user.IsApproved = this.IsApproved;
                    if (user.LastActivityDate != this.LastActivityDate) user.LastActivityDate = this.LastActivityDate;
                    if (user.LastLoginDate != this.LastLoginDate) user.LastLoginDate = this.LastLoginDate;

                    return user;
                }
    }

//Roles
    public class MembershipServiceRole {

            public MembershipServiceRole() { }

            public MembershipServiceRole(string rolename) {
                RoleName = rolename;
            }

            [Key]
            [Editable(true, AllowInitialValue = true)]
            public string RoleName { get; set; }
    }

其次,创建一个派生自 DomainService 的类来操作用户和角色包装器:

[EnableClientAccess(RequiresSecureEndpoint = false /* This should be set to true before the application is deployed */)]
    public class MembershipService : DomainService
    {
        protected override void OnError(DomainServiceErrorInfo errorInfo)
        {
            TimeoutHelper.HandleAuthenticationTimeout(errorInfo, this.ServiceContext.User);
        }

        [RequiresRole("Administrator")]
        public IEnumerable<MembershipServiceUser> GetUsers()
        {
            return Membership.GetAllUsers().Cast<MembershipUser>().Select(u => new MembershipServiceUser(u));
        }

        [RequiresRole("Administrator")]
        public IEnumerable<MembershipServiceUser> GetUsersByEmail(string email)
        {
            return Membership.FindUsersByEmail(email).Cast<MembershipUser>().Select(u => new MembershipServiceUser(u));
        }

        [RequiresRole("Administrator")]
        public MembershipServiceUser GetUsersByName(string userName)
        {
            MembershipServiceUser retVal = null;            
            retVal =  Membership.FindUsersByName(userName)
                .Cast<MembershipUser>()
                .Select(u => new MembershipServiceUser(u))
                .FirstOrDefault();
            return retVal;            
        }

        [Invoke(HasSideEffects = true)]
        public void CreateUser(MembershipServiceUser user, string password)
        {
            if (string.IsNullOrEmpty(user.Email)) {
                user.Email = "cambiar@dominio.com";
            }
            Membership.CreateUser(user.UserName, password, user.Email);
        }

        [RequiresRole("Administrator")]
        public void DeleteUser(MembershipServiceUser user)
        {
            Membership.DeleteUser(user.UserName);
        }

        [RequiresRole("Administrator")]
        public void UpdateUser(MembershipServiceUser user)
        {
            Membership.UpdateUser(user.ToMembershipUser());
        }

        [RequiresRole("Administrator")]
        [Update(UsingCustomMethod = true)]
        public void ChangePassword(MembershipServiceUser user, string newPassword)
        {
            MembershipUser u = user.ToMembershipUser();
            u.ChangePassword(u.ResetPassword(), newPassword);
        }

        [RequiresRole("Administrator")]
        public void ResetPassword(MembershipServiceUser user)
        {
            user.ToMembershipUser().ResetPassword();
        }

        [RequiresRole("Administrator")]
        public void UnlockUser(MembershipServiceUser user)
        {
            user.ToMembershipUser().UnlockUser();
        }

        #region Roles

        [RequiresRole("Administrator")]
        public IEnumerable<MembershipServiceRole> GetRoles() {
            return Roles.GetAllRoles().Cast<string>().Select(r => new MembershipServiceRole(r));
        }

        [RequiresRole("Administrator")]
        public IEnumerable<MembershipServiceRole> GetRolesForUser(string userName) {
            return Roles.GetRolesForUser(userName).Cast<string>().Select(r => new MembershipServiceRole(r));
        }

        [RequiresRole("Administrator")]
        public void CreateRole(MembershipServiceRole role) {
            Roles.CreateRole(role.RoleName);
        }

        [RequiresRole("Administrator")]
        public void DeleteRole(MembershipServiceRole role) {
            Roles.DeleteRole(role.RoleName);            
        }

        [RequiresRole("Administrator")][Invoke]
        public void AddUserToRole(string userName, string roleName) {
            if (!Roles.IsUserInRole(userName,roleName))
                Roles.AddUserToRole(userName, roleName);
        }

        [RequiresRole("Administrator")]
        [Invoke]
        public void RemoveUserFromRole(string userName, string roleName) {
            if (Roles.IsUserInRole(userName, roleName))
                Roles.RemoveUserFromRole(userName, roleName);
        }

        #endregion //Roles
    }

第三:以与您的域名类别相同的方式使用该服务 干杯!!!

于 2012-11-30T19:15:47.710 回答