把Membership,Role包装为WCF服务(ZT)

  不废话了。翠花,上代码:

ServiceContracts:
  IMembershipService.cs:
  
 1 using  System;
 2 using  System.Web.Security;
 3 using  System.ServiceModel;
 4
 5 namespace  Infrastructure.ServiceContracts
 6 {
 7    [ServiceContract(Namespace = http://Infrastructure.ServiceContracts/, Name = "IMembershipService", SessionMode = SessionMode.Allowed)]
 8    public interface IMembershipService
 9    {
10        [OperationContract]
11        MembershipUser CreateUser(string username, string password);
12        
13        [OperationContract(Name = "CreateUserWithEmail")]
14        MembershipUser CreateUser(string username, string password, string email);
15       
16        [OperationContract(Name = "CreateUserWithQuestion")]
17        MembershipUser CreateUser(string username, string password, string email,
18            string passwordQuestion,
19            string passwordAnswer,
20            bool isApproved,
21            out MembershipCreateStatus status
22            );
23       
24        [OperationContract(Name = "CreateUserWithProviderUserKey")]
25        MembershipUser CreateUser(string username, string password, string email,
26            string passwordQuestion,
27            string passwordAnswer,
28            bool isApproved,
29            Object providerUserKey,
30            out MembershipCreateStatus status
31            );
32
33        [OperationContract(Name = "DeleteUserDefault")]
34        bool DeleteUser(string username);
35
36        [OperationContract]
37        bool DeleteUser(string username, bool deleteAllRelatedData);
38
39        [OperationContract]
40        MembershipUserCollection GetAllUsers();
41
42        [OperationContract(Name = "GetAllUsersByPaging")]
43        MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords);
44
45        [OperationContract]
46        MembershipUser GetUser();
47
48        [OperationContract(Name = "GetUserIsOnline")]
49        MembershipUser GetUser(bool userIsOnline);
50
51        [OperationContract(Name = "GetUserByProviderUserKey")]
52        MembershipUser GetUser(Object providerUserKey);
53
54        [OperationContract(Name = "GetUserByUsername")]
55        MembershipUser GetUser(string username);
56
57        [OperationContract(Name = "GetUserByKeyAndIsOnline")]
58        MembershipUser GetUser(Object providerUserKey, bool userIsOnline);
59
60        [OperationContract(Name = "GetUserByNameAndIsOnline")]
61        MembershipUser GetUser(string username, bool userIsOnline);
62
63        [OperationContract]
64        string GetUserNameByEmail(string emailToMatch);
65
66        [OperationContract]
67        void UpdateUser(MembershipUser user);
68
69        [OperationContract]
70        bool ValidateUser(string username, string password);
71    }
72}

73

ServiceImplementation:
  MembershipService.cs:
    
  1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.Text;
  4 using  System.ServiceModel;
  5 using  System.Web.Security;
  6 using  Infrastructure.ServiceContracts;
  7
  8 namespace  CoralMIS.Infrastructure.ServiceImplementation
  9 {
 10    [ServiceBehavior(Name = "MembershipService",
 11       Namespace = http://Infrastructure.ServiceImplementation/2007/06)]
 12    public class MembershipService : IMembershipService
 13    {
 14        #region IMembershipService 成员
 15
 16        #region CreateUser
 17
 18        public MembershipUser CreateUser(string username, string password)
 19        {
 20            return Membership.CreateUser(username, password);
 21        }

 22
 23        public MembershipUser CreateUser(string username, string password, string email)
 24        {
 25            return Membership.CreateUser(username, password, email);
 26        }

 27
 28        public MembershipUser CreateUser(string username, string password, string email, string passwordQuestion,
 29                                         string passwordAnswer, bool isApproved, out MembershipCreateStatus status)
 30        {
 31            return
 32                Membership.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved,
 33                                      out status);
 34        }

 35
 36        public MembershipUser CreateUser(string username, string password, string email, string passwordQuestion,
 37                                         string passwordAnswer, bool isApproved, object providerUserKey,
 38                                         out MembershipCreateStatus status)
 39        {
 40            return
 41                Membership.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved,
 42                                      providerUserKey, out status);
 43        }

 44
 45        #endregion

 46
 47        DeleteUser
 60
 61        GetAllUsers
 74
 75        GetUser
108
109        public string GetUserNameByEmail(string emailToMatch)
110        {
111            return Membership.GetUserNameByEmail(emailToMatch);
112        }

113
114        public void UpdateUser(MembershipUser user)
115        {
116            Membership.UpdateUser(user);
117        }

118
119        public bool ValidateUser(string username, string password)
120        {
121            return Membership.ValidateUser(username, password);
122        }

123
124        #endregion

125    }

126}


  至于Role的实现,请参考上面。app.config,web.config中的<System.Web>原来怎么配置现在还怎么配置,我在这里只是简单地给出了一个思路,也可以很方便的改为Web Service,Remoting服务包装。欢迎大家拍砖:)

BTW:《Programming.WCF.Services》这本书里的Credentials Manager utility也有上Membership和Role的包装,可是在我看来过于复杂了:),所以自己做了上面的工作。PWS这本书的随书代码(WCF的一些扩展,一些工具,帮助类)写得很不错,我手上有,不知道是否应该放出来跟大家Sharing,请各位指点,谢谢。

你可能感兴趣的:(role)