int - 身份将 GUID 更改为 int

标签 int guid asp.net-identity

如何更改AspNetUser的PK列来自 guid 的表格至int数据类型?
现在应该可以使用最新的 asp.net-identity今天发布的版本。

但是我在任何地方都找不到这是如何完成的?

最佳答案

默认情况下,ASP.NET 标识(使用 Entity Framework )使用字符串作为主键,而不是 GUID,但它确实将 GUID 存储在这些字符串中。

您需要再定义几个类,我刚刚创建了一个新项目(我使用的是 VS2013 Update 2 CTP),以下是您需要更改的身份模型:

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationUserRole : IdentityUserRole<int>
{
}

public class ApplicationUserLogin : IdentityUserLogin<int>
{
}

public class ApplicationUserClaim : IdentityUserClaim<int>
{
}

public class ApplicationRole : IdentityRole<int, ApplicationUserRole>
{
}

public class ApplicatonUserStore :
    UserStore<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicatonUserStore(ApplicationDbContext context)
        : base(context)
    {
    }
}

public class ApplicationDbContext
    : IdentityDbContext<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}

您还需要更新其他一些地方,只需遵循编译错误,最常见的更改是需要转换返回形式 User.Identity.GetUserId() 的字符串为整数。

另外,在回答 another question (虽然我确实问了这个问题),我提供了一个示例解决方案,请参阅下面的存储库:

https://github.com/JSkimming/AspNet.Identity.EntityFramework.Multitenant

关于int - 身份将 GUID 更改为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22547712/

相关文章:

sql - 将 getdate() 转换为 int

c - 在 int 和 double 之间进行运算

c++ - 如何在 C++ 中使用 HidD_GetHidGuid()?

c# - AssemblyInfo.cs 中的 C# 程序中的 GUID 是什么?

c# - ASP.Net Identity 内置函数与 ASP.Net Core 中的自定义表

Javascript 对象,在 C 中制作类似

c - 将 int 指针类型转换为 float 指针

powershell - 使用PowerShell将GUID字符串转换为octetBytes

c# - ASP.NET Identity 2.0 中基于 token 的登录逻辑更改

c# - 在没有 IUserPasswordStore 的情况下实现 ASP.NET Identity IUserLockoutStore