【问题标题】:How do I make my wcf services available for particular user?如何使我的 wcf 服务对特定用户可用?
【发布时间】:2011-11-27 17:59:33
【问题描述】:

我正在使用 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;
    }
}

我们将不胜感激。

【问题讨论】:

  • 我正在使用 NHibernate 访问数据库!
  • 如何为特定用户采用我的服务?如果用户 x 登录,我想显示与用户 x 相关的数据。我不知道如何将用户名传递给服务!有点像会话变量。

标签: c# wcf .net-3.5


【解决方案1】:

执行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。作者似乎对问题非常敏感——你可能想给他一些指点!祝你好运!

【讨论】:

  • 感谢您的回复。你明白了,我正在使用 WCF REST 服务工具包!我在凭据上有一些错误。我在 OperationContext 中实现了这段代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多