开发者

How to tell what context you are running under in C#?

How do I to tell what context an applicatio开发者_如何转开发n is running under?

That is, user context, system context, etc... Is there some code I can run to tell? P/Invoke or something?


Look at the .NET class ServiceSecurityContext.Current -> http://msdn.microsoft.com/en-us/library/system.servicemodel.servicesecuritycontext.aspx

E.g. The following example from MSDN shows you how to use this class

// Run this method from within a method protected by the PrincipalPermissionAttribute
// to see the security context data, including the primary identity.
public void WriteServiceSecurityContextData(string fileName)
{
    using (StreamWriter sw = new StreamWriter(fileName))
    {
        // Write the primary identity and Windows identity. The primary identity is derived from
        // the credentials used to authenticate the user. The Windows identity may be a null string.
        sw.WriteLine("PrimaryIdentity: {0}", ServiceSecurityContext.Current.PrimaryIdentity.Name);
        sw.WriteLine("WindowsIdentity: {0}", ServiceSecurityContext.Current.WindowsIdentity.Name);
        sw.WriteLine();
        // Write the claimsets in the authorization context. By default, there is only one claimset
        // provided by the system. 
        foreach (ClaimSet claimset in ServiceSecurityContext.Current.AuthorizationContext.ClaimSets)
        {
            foreach (Claim claim in claimset)
            {
                // Write out each claim type, claim value, and the right. There are two
                // possible values for the right: "identity" and "possessproperty". 
                sw.WriteLine("Claim Type = {0}", claim.ClaimType);
                sw.WriteLine("\t Resource = {0}", claim.Resource.ToString());
                sw.WriteLine("\t Right = {0}", claim.Right);
            }
        }
    }
}

Source: http://msdn.microsoft.com/en-us/library/aa347790.aspx


You could check on

var isAnonymous = ServiceSecurityContext.Anonymous.IsAnonymous;

to avoid null on ServiceSecurityContext.Current

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜