How to Access Security Information in a WCF Service?

The ServiceSecurityContext class represents the security context of the remote party that is being communicated with. On the client side, it represents the identity of the service with which the client application is interacting. On the service side, it represents the identity of the client application that invoked the operation. The following code examples show how a service can access the Windows identity of the calling client application.

[Visual Basic]

If ServiceSecurityContext.Current Is Nothing Then

     Console.WriteLine("Called without a security context")


     Console.WriteLine("Windows identity is {1}", ServiceSecurityContext.Current.WindowsIdentity.Name)

 End If


[Visual C#]

if (ServiceSecurityContext.Current == null)


    Console.WriteLine("Called without a security context");




    Console.WriteLine("Windows identity is {1}", ServiceSecurityContext.Current.WindowsIdentity.Name);


For more information about programmatically accessing the security context, see How To: Examine the Security Context