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
    }

3番目:ドメインのクラスと同じようにサービスを使用してください。

于 2012-11-30T19:15:47.710 に答える