Many security and access denied problems relate to the identity used for resource access. The following code samples presented in this section can be used to help determine identity in Web pages, COM objects, and Web services.
For more information about .NET identity variables, see “ASP.NET Identity Matrix” in the Reference section of this book.
The following script can be used to gather security context related information and indicates the identity being used to run a Web page.
To use this code, copy and paste it to create a file with an .aspx file extension. Copy the file to an IIS virtual directory and view the page from a browser.
<%@ Page language="c#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Security.Principal" %>
<HTML>
<HEAD>
<title>WhoAmI</title>
</HEAD>
<body>
<form id="WhoAmI" method="post" runat="server">
<TABLE id=contextTable border=1>
<TR>
<TD align=middle colSpan=3 rowSpan="">
HttpContext.Current.User.Identity</TD>
</TR>
<TR>
<TD><b>Name</b></TD>
<TD><asp:Label ID="contextName" Runat=server /></TD>
</TR>
<TR>
<TD><b>IsAuthenticated</b></TD>
<TD><asp:Label ID="contextIsAuth" Runat=server /></TD>
</TR>
<TR>
<TD><b>AuthenticationType</b></TD>
<TD><asp:Label ID="contextAuthType" Runat=server /></TD>
</TR>
</TABLE>
<br><br>
<TABLE id=windowsIdentityTable border=1>
<TR>
<TD align=middle colSpan=3 rowSpan="">WindowsIdentity.GetCurrent()</TD>
</TR>
<TR>
<TD><b>Name</b></TD>
<TD><asp:Label ID="windowsName" Runat=server /></TD>
</TR>
<TR>
<TD><b>IsAuthenticated</b></TD>
<TD><asp:Label ID="windowsIsAuth" Runat=server /></TD>
</TR>
<TR>
<TD><b>AuthenticationType</b></TD>
<TD><asp:Label ID="windowsAuthType" Runat=server /></TD>
</TR>
</TABLE>
<br><br>
<TABLE id=threadIdentityTable border=1>
<TR>
<TD align=middle colSpan=3
rowSpan="">Thread.CurrentPrincipal.Identity</TD>
</TR>
<TR>
<TD><b>Name</b></TD>
<TD><asp:Label ID="threadName" Runat=server /></TD>
</TR>
<TR>
<TD><b>IsAuthenticated</b></TD>
<TD><asp:Label ID="threadIsAuthenticated" Runat=server /></TD>
</TR>
<TR>
<TD><b>AuthenticationType</b></TD>
<TD><asp:Label ID="threadAuthenticationType" Runat=server /></TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
<script runat=server>
void Page_Load(Object sender, EventArgs e)
{
IIdentity id = HttpContext.Current.User.Identity;
if(null != id)
{
contextName.Text = id.Name;
contextIsAuth.Text = id.IsAuthenticated.ToString();
contextAuthType.Text = id.AuthenticationType;
}
id = Thread.CurrentPrincipal.Identity;
if(null != id)
{
threadName.Text = id.Name;
threadIsAuthenticated.Text = id.IsAuthenticated.ToString();
threadAuthenticationType.Text = id.AuthenticationType;
}
id = WindowsIdentity.GetCurrent();
windowsName.Text = id.Name;
windowsIsAuth.Text = id.IsAuthenticated.ToString();
windowsAuthType.Text = id.AuthenticationType;
}
</script>
The following code can be used within a Web service to obtain identity information.
[WebMethod]
public string GetDotNetThreadIdentity()
{
return Thread.CurrentPrincipal.Identity.Name;
}
[WebMethod]
public string GetWindowsThreadIdentity()
{
return WindowsIdentity.GetCurrent().Name;
}
[WebMethod]
public string GetUserIdentity()
{
return User.Identity.Name;
}
[WebMethod]
public string GetHttpContextUserIdentity()
{
return HttpContext.Current.User.Identity.Name;
}
For a list of all ASP.NET security related Knowledge Base articles, go to http://support.microsoft.com, click “Advanced Search and Help” and search under ASP.NET security.
For a list of security related articles that deal with frequently seen error messages, use the following search keywords:
prb kbsecurity kbaspnet
The following method can be used to return the identity of a Visual Basic 6 COM object. You can call Visual Basic 6.0 COM objects directly from ASP.NET applications through COM interop. The following method can be helpful when you need to troubleshoot access denied errors from your component when it attempts to access resources.
Private Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Function WhoAmI()
Dim sBuff As String
Dim lConst As Long
Dim lRet As Long
Dim sName As String
lConst = 199
sBuff = Space$(200)
lRet = GetUserName(sBuff, lConst)
WhoAmI = Trim$(Left$(sBuff, lConst))
End Function