سؤال

So far I've had no issues converting all the types to use Guid (e.g):

public class UserLogin : IdentityUserLogin<Guid> { ... }

public class UserRole : IdentityUserRole<Guid> { ... }

public class UserClaim : IdentityUserClaim<Guid> { ... }

public class User : IdentityUser<Guid, UserLogin, UserRole, UserClaim>
{
  ...
}

public class UserManager : UserManager<User, Guid> { ... }

Taking a deeper look at Microsoft.AspNet.Identity.UserManager:

public class UserManager<TUser, TKey> : IDisposable
  where TUser : class, Microsoft.AspNet.Identity.IUser<TKey>
  where TKey : System.IEquatable<TKey>
{ ... }

Obviously the TKey is generic so it allows Guid.

Next is the Role Manager:

public class RoleManager : RoleManager<Role>
{
}

But the definition of the Microsoft.AspNet.Identity.RoleManager:

public class RoleManager<TRole> : RoleManager<TRole, string> 
  where TRole : class, Microsoft.AspNet.Identity.IRole<string>
{
}

Appears to only work if the key for role is string. Is this a bug or am I missing something?

هل كانت مفيدة؟

المحلول

Ok this is quite odd.

// Assembly Microsoft.AspNet.Identity.Core.dll, v2.0.0.0
namespace Microsoft.AspNet.Identity
{
  public class RoleManager<TRole> : RoleManager<TRole, string> 
    where TRole : class, Microsoft.AspNet.Identity.IRole<string>
  {
    // Summary:
    //     Constructor
    //
    // Parameters:
    //   store:
    public RoleManager(IRoleStore<TRole, string> store);
  }
}

But that derives from another RoleManager!

// Assembly Microsoft.AspNet.Identity.Core.dll, v2.0.0.0
namespace Microsoft.AspNet.Identity
{
  public class RoleManager<TRole, TKey> : IDisposable
    where TRole : class, global::Microsoft.AspNet.Identity.IRole<TKey>
    where TKey : global::System.IEquatable<TKey>
  {
    ...
  }
}

Maybe the first role manager is some legacy code (string for Asp.Net Identity 1). Deriving from the second RoleManager seems to be the way to go:

public class RoleManager : RoleManager<Role, Guid> { ... } 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top