Windows身份验证 - Guid应该包含32个带有4个破折号的数字(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

6

我刚开始使用stackify的Retrace来监控我的应用程序,发现有成千上万的错误,这些错误包括:

System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
at System.Guid.TryParseGuidWithNoStyle
at System.Guid.TryParseGuid
at System.Guid..ctor
at System.DirectoryServices.AccountManagement.ADStoreCtx.IdentityClaimToFilter

这些错误每天发生数千次,我无法完全理解其原因。首先,我的应用程序的工作方式如下:

MVC前端 - 使用Windows身份验证(使用RestSharp调用后端)

Web API后端 - 使用从RestSharp NTLM身份验证传递的Windows身份验证。

RestSharp包装器

    public object WebRequest<T>(string controller, Dictionary<string, string> parameters, Method apiMethod, string action)
    {
        RestClient client = new RestClient(Url + controller + "/");
        client.Authenticator = new NtlmAuthenticator();
        RestRequest request = new RestRequest(action, apiMethod);

        if (parameters != null && parameters.Count > 0)
        {
            foreach (var parameter in parameters)
            {
                request.AddParameter(parameter.Key, parameter.Value);
            }
        }

        object result = JsonToObject<T>(client.Execute(request).Content);

        return result;
    }

辅助方法

@helper Username()
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

 var username = System.Web.HttpContext.Current.User.Identity.Name.Replace(@"DOMAIN\", "");

@username
}

@helper UserFullName()
{
    using (var context = new PrincipalContext(ContextType.Domain))
    {
        var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name);
        if (principal != null)
        {
            var fullName = string.Format("{0}", principal.DisplayName);
            @fullName
        }
    }
}

有没有关于这个错误发生在哪里或者我可以做什么来缩小范围的建议?从Stackify上看,它似乎在每个页面都会发生。

尝试在使用Guid时调试每个单独的点。Guid.TryParse指出它格式不正确。 - Gonzo345
感谢您的评论,我的代码在前端应用程序中没有创建任何GUID,这也是错误的原因。 - Harambe
尝试使用 UserPrincipal.FindByIdentity(context, IdentityType.Name, User.Identity.Name); - 因为您当前没有指定一个(GUID 是一个有效的选项),也许非特定重载存在问题。 - Alex K.
现在将其放入并报告回来。谢谢。 - Harambe
@AlexK,请将此作为答案发布,IdentityType.SamAccountName 给出了结果,谢谢! - Harambe
1个回答

3

有一个重载的 FindByIdentity 可以让您指定 identityValue 的实际含义,例如:

Try UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, User.Identity.Name);

由于GUID是这个调用的有效选项,因此当您使用非特定的重载时似乎存在问题,并且它可能会尝试嗅探值是什么。


谢谢你的回答,Alex。非常好的答案,不过我使用了IdentityType.SamAccountName。谢谢。 - Harambe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接