Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为特定用户提供wcf服务?_C#_Wcf_.net 3.5 - Fatal编程技术网

C# 如何为特定用户提供wcf服务?

C# 如何为特定用户提供wcf服务?,c#,wcf,.net-3.5,C#,Wcf,.net 3.5,我正在使用CustomMembershipProvider验证我的WCF服务的用户。用户访问该服务后,将收到提示,提示输入其凭据 注意:我想我可以得到用户名并识别用户。但如何将变量从CustomMembershipProvider传递到我的wcf服务文件 自定义成员资格提供程序代码 这是我正在验证的代码 public override bool ValidateUser(string username, string password) { int authenticat

我正在使用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;
    }
这是我的基本身份验证主机工厂

这将调用my 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;
    }
}
任何帮助都将不胜感激。

执行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访问用户名


我想你是从这篇文章开始工作的。作者似乎对问题反应非常积极——你可能想向他寻求一些建议!祝你好运

我在用NHibernate访问数据库!如何对特定用户采用我的服务?如果用户x登录,我想显示与用户x相关的数据。我不知道如何将用户名传递给服务!有点像会话变量。谢谢您的回复。你明白了,我正在使用WCF REST服务包!我在凭证上有一些错误。我在OperationContext中实现了这段代码。