It is often seen that we need to dump all the request variables such as headers and all to do troubleshooting. Many things can be dumped using IIS logs itself either by enabling those or by adding extended data fields. Refer below link for details on extended logging.
https://docs.microsoft.com/en-us/iis/extensions/advanced-logging-module/advanced-logging-for-iis-custom-logging
But in some scenarios that is not enough. For example to ensure that the user name / alias is correctly obtained in ASP.Net code. In those scenarios, it is better to dump using custom code. Below is a code snippet which uses ASP.Net template techniques to dump http request and environment variables such as user name etc...
It doesn't need any code behind or pre-compilation. Just save this as file with .aspx extension say dump.aspx.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Language="C#" AutoEventWireup="true" Trace="true" TraceMode="SortByCategory" %> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head runat="server"> | |
<title>ASP.NET Diagnostic Page</title> | |
</head> | |
<body> | |
<form id="form1" runat="server"> | |
<% | |
void DumpIdentityAsTable(object identity) | |
{ | |
try | |
{ | |
Response.Write("<table>"); | |
System.Reflection.MemberInfo[] refl_WindowsIdenty_members = identity.GetType().FindMembers( | |
System.Reflection.MemberTypes.Property, | |
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, | |
delegate (System.Reflection.MemberInfo objMemberInfo, Object objSearch) { return true; }, | |
null); | |
foreach (System.Reflection.MemberInfo currentMemberInfo in refl_WindowsIdenty_members) | |
{ | |
string key = currentMemberInfo.Name, value = string.Empty; | |
System.Reflection.MethodInfo getAccessorInfo = ((System.Reflection.PropertyInfo)currentMemberInfo).GetGetMethod(); | |
value = GetObjectValueAsString(getAccessorInfo.Invoke(identity, null)); | |
Response.Write(string.Format("<tr><td>{0}</td><td>{1}</td></tr>", key, value)); | |
} | |
} | |
catch { } | |
finally { Response.Write("</table>"); } | |
} | |
string GetObjectValueAsString(object obj) | |
{ | |
string value = string.Empty; | |
if (typeof(IEnumerable).IsInstanceOfType(obj) && !typeof(string).IsInstanceOfType(obj)) | |
{ | |
foreach (object item in (IEnumerable)obj) | |
{ | |
value += item.ToString() + "<br />"; | |
} | |
} | |
else | |
{ | |
value = obj.ToString(); | |
} | |
return value; | |
} | |
string GetAuthenticationMethod() | |
{ | |
try | |
{ | |
if (Request.ServerVariables["AUTH_TYPE"] == "Negotiate") | |
{ | |
if (Request.Headers["Authorization"].Length > 1000) | |
return "KERBEROS"; | |
else | |
return "NTLM"; | |
} | |
else return string.Empty; | |
} | |
catch { return string.Empty; } | |
} | |
%> | |
<h2>Environment Variables</h2> | |
<table> | |
<% | |
foreach (DictionaryEntry entry in Environment.GetEnvironmentVariables()) | |
{ | |
Response.Write(string.Format("<tr><td>{0}</td><td>{1}</td></tr>", entry.Key, entry.Value)); | |
} | |
%> | |
</table> | |
<h2>Request.Headers</h2> | |
<h2>Authentication</h2> | |
System.Environment.UserName = <%= System.Environment.UserName %><br /> | |
Request.ServerVariables["AUTH_TYPE"] = <%= Request.ServerVariables["AUTH_TYPE"] %><br /> | |
Request.ServerVariables["AUTH_USER"] = <%=Request.ServerVariables["AUTH_USER"] %><br /> | |
Authentication method = <%= GetAuthenticationMethod() %><br /> | |
HttpContext.Current.Request.LogonUserIdentity.Name = <%= HttpContext.Current.Request.LogonUserIdentity.Name %><br /> | |
HttpContext.Current.Request.IsAuthenticated = <%= HttpContext.Current.Request.IsAuthenticated %><br /> | |
HttpContext.Current.User.Identity.Name = <%= HttpContext.Current.User.Identity.Name %><br /> | |
<h3>Dumping HttpContext.Current.User.Identity</h3> | |
<%DumpIdentityAsTable(HttpContext.Current.User.Identity);%> | |
Security.Principal.WindowsIdentity.GetCurrent().Name= <%= System.Security.Principal.WindowsIdentity.GetCurrent().Name %><br /> | |
<h3>Dumping System.Security.Principal.WindowsIdentity.GetCurrent()</h3> | |
<%DumpIdentityAsTable(System.Security.Principal.WindowsIdentity.GetCurrent());%> | |
System.Threading.Thread.CurrentPrincipal.Identity.Name = <%= System.Threading.Thread.CurrentPrincipal.Identity.Name %><br /> | |
<h3>Dumping System.Threading.Thread.CurrentPrincipal.Identity</h3> | |
<% DumpIdentityAsTable(System.Threading.Thread.CurrentPrincipal.Identity); %> | |
<h2>Threading </h2> | |
System.Threading.Thread.CurrentThread.ManagedThreadId = <%= System.Threading.Thread.CurrentThread.ManagedThreadId %><br /> | |
System.Threading.Thread.CurrentThread.CurrentCulture.Name = <%= System.Threading.Thread.CurrentThread.CurrentCulture.Name %><br /> | |
<h2>Application</h2> | |
Request.ApplicationPath = <%= Request.ApplicationPath %><br /> | |
Request.PhysicalApplicationPath = <%= Request.PhysicalApplicationPath %><br /> | |
Request.PhysicalPath = <%= Request.PhysicalPath %><br /> | |
<h2>Misc</h2> | |
Response.Filter = <%= Request.Filter.ToString() %><br /> | |
Request.UrlReferrer = <%= Request.UrlReferrer %><br /> | |
Request.UserLanguages = <%= string.Join(",", (Request.UserLanguages ?? new string[0])) %><br /> | |
</form> | |
<h2>Trace details</h2> | |
</body> | |
</html> |
Running the dump.aspx
Place into the folder and browse that file ashttp://<FQDN>/<app name if applicable>/dump.aspx
eg: https://localhost/MyApp/dump.aspx
It is advised just put this file when we debug and not to add as part of application. But if permitted check in this file into source control so that the troubleshooting will be much easier.
This is tested mainly in .Net Full Framework. Yet to check with .Net Core.
References
The problem has already been faced by many and there are already enough links where people shared the solution. Since every solution is unique putting one more along with some below.
https://code.msdn.microsoft.com/ASPNET-Diagnostic-page-to-9a029c20
https://pedrosneglectedtechblog.blogspot.com/2010/02/quick-and-easy-http-header-dump-aspnet.html
https://blogs.msdn.microsoft.com/friis/2013/01/08/asp-net-authentication-test-page/
No comments:
Post a Comment