Silverlight& RIA服务:管理用户

时间:2012-10-31 12:10:50

标签: silverlight-5.0

我想在客户端管理用户和角色,但我找不到完成此任务的方法。链接或代码将非常感激。

1 个答案:

答案 0 :(得分: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
    }

第三:使用与域名clases相同的服务 干杯!!!