Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Authentication - Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

I have just started using Retrace by stackify to monitor my application and have seen thousands of errors which are:

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

These errors are happening thousands of times a day and I can't work out quite why. Firstly, my application works like so:

MVC front end - using Windows Authentication (using RestSharp to call backend)

Web API back end, using Windows Authentication passed from RestSharp NTLM Authentication.

RestSharp wrapper

    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 Methods

@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
        }
    }
}

Any suggestions on where this error may be happening or what I can do to narrow it down? It seems to happen on every page from what I can tell in Stackify.

like image 677
Harambe Avatar asked Jun 22 '18 11:06

Harambe


1 Answers

There is an overload of FindByIdentity that allows you to specify what the identityValue actually is, E.g.

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

As GUID is valid option for this call it seems there is an issue when you use the non-specific overload & it presumably attempts to sniff what the value is.

like image 51
Alex K. Avatar answered Oct 16 '22 02:10

Alex K.