为什么UserPrincipal.FindByIdentity返回有关GUID为32位的错误?

Jon*_*ter 4 c#

我的应用程序使用UserPrincipal该类来确定用户所属的组,然后使用该信息来确定用户是否经过身份验证以使用我的应用程序.事情一直很好,但最近我开始得到一个例外

Guid应该包含32位数字和4个破折号(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

当调用UserPrincipal.FindByIdentity.似乎呼叫成功并且异常正在得到妥善处理,但令我感到紧张的是,身份验证将来会突然中断.我没有在任何地方明确创建GUID,所以我不知道异常来自哪里.

D S*_*ley 6

最有可能的是,在试图从无效的GUID值初始化某种安全描述符的Framework代码深处抛出异常.如果框架正在捕获它并在内部处理它我不会担心它.

跟踪框架代码,这是一个可能发生的地方:

protected static bool IdentityClaimToFilter(string identity, string identityFormat, ref string filter, bool throwOnFail)
{
  if (identity == null)
    identity = "";
  StringBuilder filter1 = new StringBuilder();
  switch (identityFormat)
  {
    case "ms-guid":
      Guid guid;
      try
      {
        guid = new Guid(identity);
      }
      catch (FormatException ex)
      {
        if (throwOnFail)
          throw new ArgumentException(ex.Message, (Exception) ex);
        else
          return false;
      }
...
Run Code Online (Sandbox Code Playgroud)

请注意,它尝试创建一个新的Guid,如果失败,则抛出异常,但代码吞下它并返回false