WindowsIdentity.GetAnonymous Method

Definition

Returns a WindowsIdentity object that you can use as a sentinel value in your code to represent an anonymous user. The property value does not represent the built-in anonymous identity used by the Windows operating system.

C#
public static System.Security.Principal.WindowsIdentity GetAnonymous();

Returns

An object that represents an anonymous user.

Examples

The following code shows the use of the GetAnonymous method to return a WindowsIdentity object that represents an anonymous Windows user. This code example is part of a larger example provided for the WindowsIdentity class.

C#
WindowsIdentity windowsIdentity = WindowsIdentity.GetAnonymous();

Remarks

This property returns an empty WindowsIdentity object that enables you to treat operations as anonymous. The property value does not correspond to a Windows anonymous user and cannot be used for impersonation. Also, note that the identity returned by this property is not static; each call to GetAnonymous returns a different anonymous identity.

You can use the IsAnonymous property to detect the return value from GetAnonymous. However, IsAnonymous detects both the Windows anonymous identity and the anonymous identity returned by this method. To use the latter identity, cache the GetAnonymous return value instead of relying on the IsAnonymous property.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5