Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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在OperationContext中设置用户ID_C#_Wcf_Service_Operationcontext - Fatal编程技术网

C# WCF在OperationContext中设置用户ID

C# WCF在OperationContext中设置用户ID,c#,wcf,service,operationcontext,C#,Wcf,Service,Operationcontext,Im在我的WCF服务应用程序中有一个自定义验证器,我使用标准的Microsoft标识表验证用户名和密码 此函数返回用户的数据,包括用户ID(Guid) 现在我想在operationContext中添加UserID,以便稍后在som其他函数中使用该UserID OperationContext.Current.ServiceSecurityContext.AuthorizationContext.Properties["UserID"] = user.UserID; 但当我这样做时,我会收到错误

Im在我的WCF服务应用程序中有一个自定义验证器,我使用标准的Microsoft标识表验证用户名和密码

此函数返回用户的数据,包括用户ID(Guid)

现在我想在operationContext中添加UserID,以便稍后在som其他函数中使用该UserID

OperationContext.Current.ServiceSecurityContext.AuthorizationContext.Properties["UserID"] = user.UserID;
但当我这样做时,我会收到错误消息“对象引用未设置为对象的实例”。这并不奇怪,因为我没有创建它的实例。但我不知道怎么做

因此,我的问题是如何从Validate函数将UserID保存在OperationContext中,以便在其他函数中重用UserID

namespace Facilicom.SOA.Service.FSE.Implementation.Authentication

{

public class IdentityValidator : UserNamePasswordValidator
{

public override void Validate(string userName, string password)

    {

        Debug.WriteLine("Check user name");
        User user = new IdentityAuthenticator().ValidateUser(userName, password);
        if (user != null)
            OperationContext.Current.ServiceSecurityContext.AuthorizationContext.Properties["UserID"] = user.UserID;
        else
        {
            var msg = String.Format("You are not authorized");
            Trace.TraceWarning(msg);
            throw new FaultException(msg);//the client actually will receive MessageSecurityException. But if I throw MessageSecurityException, the runtime will give FaultException to client without clear message.
        }
       }
    } 
}
namespace Facilicom.SOA.Service.FSE.Implementation.Authentication

{

public class IdentityValidator : UserNamePasswordValidator
{

public override void Validate(string userName, string password)

    {

        Debug.WriteLine("Check user name");
        User user = new IdentityAuthenticator().ValidateUser(userName, password);
        if (user != null)
            OperationContext.Current.ServiceSecurityContext.AuthorizationContext.Properties["UserID"] = user.UserID;
        else
        {
            var msg = String.Format("You are not authorized");
            Trace.TraceWarning(msg);
            throw new FaultException(msg);//the client actually will receive MessageSecurityException. But if I throw MessageSecurityException, the runtime will give FaultException to client without clear message.
        }
       }
    } 
}