检索REMOTE_USER服务器变量时出现异常

时间:2011-03-04 12:49:20

标签: c# asp.net iis

我们有一个ashx http处理程序,可以在ProcessRequest期间从ServerVariables中检索REMOTE_USER变量。

public void ProcessRequest(HttpContext context)
{
    if (context.Request.ServerVariables["REMOTE_USER"].Contains("\\")) // exception is thrown here
    ...

身份验证由IIS使用基本身份验证(域控制器上的Windows帐户)处理。

对于某些用户,这开始失败(在几个小时后正常工作)并抛出以下异常:

[IdentityNotMappedException: Some or all identity references could not be translated.]
   System.Security.Principal.SecurityIdentifier.Translate(IdentityReferenceCollection sourceSids, Type targetType, Boolean forceSuccess) +7608560
   System.Security.Principal.SecurityIdentifier.Translate(Type targetType) +100
   System.Security.Principal.WindowsIdentity.GetName() +164
   System.Security.Principal.WindowsIdentity.get_Name() +31
   System.Web.HttpRequest.CalcDynamicServerVariable(DynamicServerVariable var) +8726378
   System.Web.HttpServerVarsCollection.GetSimpleServerVar(String name) +424
   System.Web.HttpServerVarsCollection.Get(String name) +8634072
   System.Collections.Specialized.NameValueCollection.get_Item(String name) +7
   Some.Namespace.AHttpHandler.ProcessRequest(HttpContext context) in c:\afolder\AHttpHandler.ashx.cs:34
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181 
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75 

我们重启此应用程序的应用程序池后,问题解决了大约一天。

有什么想法吗? 我们应该使用另一个服务器变量来检索用户名吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

让框架为你做脏工作怎么样?

context.User.Identity.Name应包含相同的数据。

相关问题