【问题标题】:Windows Authentication - Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)Windows 身份验证 - Guid 应包含 32 位数字和 4 个破折号 (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
【发布时间】:2018-06-22 11:15:16
【问题描述】:

我刚刚开始使用 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 说它的格式不正确。
  • 感谢您的评论,我的代码在错误所在的前端应用程序中没有创建 guid。
  • 试试UserPrincipal.FindByIdentity(context, IdentityType.Name, User.Identity.Name); - 因为您当前没有指定一个(GUID 是一个有效的选项),也许非特定重载有问题。
  • 现在将其放入并报告。谢谢。

标签: c# asp.net .net asp.net-mvc restsharp


【解决方案1】:

FindByIdentity 的重载允许您指定 identityValue 的实际含义,例如

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

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

【讨论】:

  • 感谢亚历克斯。很好的答案,但是我确实使用了 IdentityType.SamAccountName。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-25
相关资源
最近更新 更多