2011-09-29 69 views
0

我正在使用CustomMembershipProvider为我的WCF服务验证用户。用户访问该服务后将获得提示。如何使我的wcf服务可供特定用户使用?

注:我想我可以得到用户名和用户名。但是如何将CustomMembershipProvider中的变量传递给我的wcf服务文件?

定义成员资格提供代码

这里是我验证码!

public override bool ValidateUser(string username, string password) 
    { 
     int authenticatedId = SecurityManager.Authenticate(username, password); 
     if (authenticatedId != -1) 
     { 
      return true; 
     } 

     return false; 
    } 

这是我的基本身份验证主机厂

这会打电话给我的CustomMembershipProvider替换默认的Web服务工厂。

public class BasicAuthenticationHostFactory : ServiceHostFactory 
{ 
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) 
    { 
     var serviceHost = new WebServiceHost2(serviceType, true, baseAddresses); 
     serviceHost.Interceptors.Add(RequestInterceptorFactory.Create("DataWebService", new CustomMembershipProvider())); 
     return serviceHost; 
    } 
} 

任何帮助将不胜感激。

+2

为什么nhibernate标签? – rbellamy

+0

那么我使用NHibernate来访问数据库! – HardCode

+0

又是什么问题? –

回答

0

一旦您执行了MembershipProvider.ValidateUser,应该创建一个有效的SecurityServiceContext并将其添加到传入请求中。

internal ServiceSecurityContext Create(Credentials credentials) 
{ 
    var authorizationPolicies = new List<iauthorizationpolicy>(); 
    authorizationPolicies.Add(authorizationPolicyFactory.Create(credentials)); 
    return new ServiceSecurityContext(authorizationPolicies.AsReadOnly()); 
} 

那么你应该有访问用户名通过ServiceSecurityContext.Current.PrimaryIdentity.Name

我认为你从文章Basic Authentication on a WCF REST Service工作。作者似乎对问题的反应非常敏感 - 您可能想要为他提供一些指导!祝你好运!

+0

谢谢你的回应。你明白了我正在使用WCF REST服务套件!我在凭证上有一些错误。我在OperationContext中实现了这个代码。 – HardCode