如何为特定用户提供我的wcf服务?

时间:2011-09-29 14:03:45

标签: c# wcf .net-3.5

我正在使用CustomMembershipProvider来验证用户的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;
    }
}

任何帮助将不胜感激。

1 个答案:

答案 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。作者似乎对问题非常敏感 - 你可能想要点击他一些指针!祝你好运!