gpt4 book ai didi

.net - 如何使用 Web API 2 + AspNet Identity 2 扩展 IdentityRole

转载 作者:行者123 更新时间:2023-12-04 23:21:20 24 4
gpt4 key购买 nike

我试图在最新版本的 Visual Studio 2013 中扩展 Web API 2 (With Individual Accounts) 模板中提供的 AspNet IdentityRole 类。当我点击/api/roles 时,它返回一个空数组

身份模型

namespace API.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser<string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>, IUser, IUser<string>
{
[NotMapped]
public virtual UserProfile Profile { get; set; }

public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }

//public virtual List<ApplicationUserRole> ApplicationRoles { get; set; }

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager,
string authenticationType)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
ClaimsIdentity userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
// Add custom user claims here
return userIdentity;
}
}

public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
public ApplicationRole() : base() { }

}

public class ApplicationUserRole : IdentityUserRole<string>
{
public ApplicationUserRole()
: base()
{ }

public virtual ApplicationRole Role { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole,string,IdentityUserLogin,ApplicationUserRole,IdentityUserClaim>

{

public ApplicationDbContext()
: base("DefaultConnection")
{
base.Configuration.ProxyCreationEnabled = false;
}

public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}

}

身份配置
namespace API
{
// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.

public class ApplicationUserManager : UserManager<ApplicationUser, string>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));

// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
IDataProtectionProvider dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}



public class ApplicationRoleManager : RoleManager<ApplicationRole, string>
{
public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store)
: base(store)
{
}

public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options,
IOwinContext context)
{
var manager = new ApplicationRoleManager(new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>()));
return manager;
}

}

}

最佳答案

你的错误在这里

ApplicationDbContext : IdentityDbContext<ApplicationUser>

您的数据库上下文的范围来自 IdentityDbContext<ApplicationUser>这是基于
IdentityDbContext<TUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>

ApplicationDbContext 将生成 IdentityRole当您真正想要时 ApplicationRole作为你的角色。

将您的 ApplicationDbContext 更改为
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>
{

public ApplicationDbContext()
: base("DefaultConnection", false)
{
base.Configuration.ProxyCreationEnabled = false;
}

public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}

在这一点上,您不必覆盖 OnModelCreating 并添加属性角色。

由于您在 ApplicationUserRole 中有导航属性,因此您应该添加配置映射
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUserRole>().HasRequired(ur => ur.Role).WithMany().HasForeignKey(ur => ur.RoleId);
}

IdentityModel.cs 应该是这样的:
public class ApplicationUserRole : IdentityUserRole<string>
{
public ApplicationUserRole()
: base()
{ }

public virtual ApplicationRole Role { get; set; }
}

public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
public ApplicationRole() : base() { }

public ApplicationRole(string name, string description)
: base()
{
this.Name = name;
this.Description = description;
}

public string Description { get; set; }
}

public class ApplicationUser : IdentityUser<string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>
{

public ApplicationDbContext()
: base("DefaultConnection")
{
base.Configuration.ProxyCreationEnabled = false;
}

public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUserRole>().HasRequired(ur => ur.Role).WithMany().HasForeignKey(ur => ur.RoleId);
}
}

identityconfig.cs 应该是这样的:
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>, IUserStore<ApplicationUser>
{
public ApplicationUserStore(ApplicationDbContext context)
: base(context)
{
}
}

public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new ApplicationUserStore(context.Get<ApplicationDbContext>()));
....
}
}
public class ApplicationRoleStore : RoleStore<ApplicationRole, string, ApplicationUserRole>
{
public ApplicationRoleStore(ApplicationDbContext context)
: base(context)
{

}
}
public class ApplicationRoleManager : RoleManager<ApplicationRole, string>
{
public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store)
: base(store)
{
}

public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options,
IOwinContext context)
{
var manager = new ApplicationRoleManager(new ApplicationRoleStore(context.Get<ApplicationDbContext>()));
return manager;
}
}

关于.net - 如何使用 Web API 2 + AspNet Identity 2 扩展 IdentityRole,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25671881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com