Tag: asp.net identity

InvalidOperationException:无法为“Role”创建DbSet,因为此类型未包含在上下文的模型中

以下解决方案适用于.net核心1.1,但在从1.1升级到2.0后,我收到以下错误: InvalidOperationException:无法为“Role”创建DbSet,因为此类型未包含在上下文的模型中。 当用户尝试登录并执行以下语句时: var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false); 怎么了? User.cs public partial class User : IdentityUser { public string Name { get; set; } } IdentityEntities.cs public partial class UserLogin : IdentityUserLogin { } public partial class UserRole : IdentityUserRole { } public partial class UserClaim : IdentityUserClaim { } public […]

Web API 2 + MVC 5共享标识表

我有一个Web API和MVC5共享相同的身份表。 属于Web API的Context: public partial class CrestfineContext: IdentityDbContext { public CrestfineContext(): base(“WelbeckDatabase”, throwIfV1Schema: false) { } protected override void OnModelCreating(DbModelBuilder builder) { builder.Conventions.Remove(); builder.Entity().ToTable(“UserClaim”).HasKey(r => r.Id); builder.Entity().ToTable(“UserLogin”).HasKey(l => l.UserId); builder.Entity().ToTable(“Role”).HasKey(r => r.Id); builder.Entity().ToTable(“User”).HasKey(r => new{ r.IDNumber, r.UserName}); builder.Entity().ToTable(“User”).HasKey(r => r.UserName); builder.Entity().ToTable(“UserRole”).HasKey(r => new { r.RoleId, r.UserId });/**/ } 属于Web应用程序(MVC 5)的Context是: public partial class CrestfineContext […]

自定义和集成自己的数据库与mvc5身份

我正在开发一个MVC项目,该项目需要使用Identity创建的身份validation和授权,我的项目数据库还包括应该使用该系统的主要实体,处理这种情况的最佳实践是什么。 我应该根据我的要求自定义身份ApplicationUser。 创建我自己的表(模型)并将它们与身份表集成,我不知道如何做到这一点。 或任何其他解决方案。

ASP身份2 + Web API令牌身份validation – 持久声明未加载

我在ASP.NET Web API Token Auth中遇到了一些问题。 基本上我创建了一个具有一些声明的用户(值存储在AspNetUserClaim表中)但是当创建用户身份时,这些声明不会从数据库中提取。 我的设置细分如下。 用户类:具有GenerateUserIdentityAsync方法(非常标准)和一些自定义属性: public class LibraryUser : IdentityUser{ //Add Custom Properties Here public string Company { get; set; } public string DisplayName { get; set; } public async Task GenerateUserIdentityAsync(UserManager manager, string authenticationType) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, […]

无法从AspNetUsers表中检索inheritance的自定义属性值

我使用ASP.NET Identity 2.0并创建从ApplicationUserinheritance的两个不同的用户实体,如在ASP.NET MVC 5中使用entity framework6实现inheritance时发布的那样,但是在到达时我无法访问这些自定义属性及其值(StudentNumber和Profession) ApplicationUser(Name,Surname)中定义的自定义属性。 想法? 这是我试图用来检索值Name , Surname , StudentNumber和Profession : 控制器: public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager) { UserManager = userManager; SignInManager = signInManager; } private ApplicationUserManager _userManager; public ApplicationUserManager UserManager { get { return _userManager ?? HttpContext.GetOwinContext() .GetUserManager(); } private set { _userManager = value; } } [AllowAnonymous] public ActionResult ListAccount(JQueryDataTableParamModel […]

用户角色/授权在ASP.NET标识中不起作用

在我们的DbContext.cs上有这个(模型构建器)代码 base.OnModelCreating(modelBuilder); modelBuilder.Entity().HasKey(l => l.UserId); modelBuilder.Entity().HasKey(r => r.Id); modelBuilder.Entity().HasKey(r => new { r.RoleId, r.UserId }); modelBuilder.Entity().ToTable(“ApplicationUser”); 除授权/用户角色外,一切正常。 检查完所有表后,我注意到IdentityUserRoles表创建了4列: RoleId,UserId,IdentityRole_Id和ApplicationUser_Id。 我发现, IdentityRole_Id和ApplicationUser_Id [外键]被映射或使用,而不是RoleId和UserId [主键]。 不幸的是,身份(Id)数据被插入到RoleId / UserId列中,IdenityRole_Id / ApplicationUser_Id默认为NULL 。 请帮忙。 我的代码: public class RqDbContext : DbContext { private const string ConnectionString = “RqDbContext”; public RqDbContext() : base(ConnectionString) { } public static RqDbContext Create() { return […]

指定密钥太长; 最大密钥长度为767字节 – ASPNet Identity MySQL

我用Identity和MySQL创建了一个MVC应用程序。 我创建了我的实体,但是当我创建用户表时,它失败并且标题中指定了错误。 我搜索过,人们说UserName , Name和Email属性太长了。 我已经厌倦了在这些列上设置最大长度,如下面的示例,但我无法让它工作。 IdentityModel: [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base(“CaptureDBContext”, throwIfV1Schema: false) { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity().Property(u => u.UserName).IsUnicode(false); modelBuilder.Entity().Property(u => u.Email).IsUnicode(false); modelBuilder.Entity().Property(r => r.Name).HasMaxLength(255); } } 有人可以帮忙吗? 我可以使用它来处理标准实体,但不能使用Identity表 这个问题不重复,因为链接中发布的教程是2年,MySQL.Data版本则不支持EF。 我正在使用的那个。

更改Asp.net标识的主键和GetUserID

我正在创建一个WebAPI(OData)项目,并使用Asp.Identity进行用户管理。 我已经阅读了ASP.NET身份中用户的更改主键 ,一切都按规定工作,我不知道如何配置Bearer令牌。 在Tom FitzMacken的示例中,他按如下方式配置Cookie身份validation。 app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString(“/Account/Login”), Provider = new CookieAuthenticationProvider { OnValidateIdentity = SecurityStampValidator .OnValidateIdentity( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager), getUserIdCallback:(id)=>(id.GetUserId())) } }); 这是我的身份validation代码,取自Mike Wasson的优秀文章, 在ASP.NET Web API 2.2中使用个人帐户和本地登录保护Web API // Configure the application for OAuth based flow PublicClientId = “self”; OAuthOptions = new […]

需要了解如何获取/显示用户角色

我正在使用Identity 2学习MVC,我正在采取一个示例项目并调整它以适应我正在尝试使用ASP.Net成员资格提供程序从WebForms转换的网站。 我现在遇到的问题是如何从另一个表中获取值以填充类中的属性。 我有一个名为AppUser.cs的类,它inheritance自IdentityUser。 这基本上映射到使用EntityFramework的名为“AspNetUsers”的表。 我使用EF迁移向该表添加了更多属性(名/姓)。 现在,我想显示用户分配的角色,最终需要获取可用角色列表并将其添加到下拉列表中。 我将RoleName属性添加到我的AppUser类,但我假设因为该数据未存储在AspNetUsers表中,这就是为什么我收到错误“无效的列名’RoleName’”。 我如何填充角色和/或获取AppUser类中的可用角色列表,以便我可以在我的视图中显示它? public class AppUser : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } public string RoleName { get; set; } } “Index.cshtml”视图上的模型是AppUser。 @model IEnumerable 这是我的Controller的ActionResult。 public ActionResult Index() { return View(UserManager.Users); } private AppUserManager UserManager { get { return […]

Asp.net核心 – 没有这样的表:AspNetUsers

所以我试图为我的asp.net核心应用程序实现用户登录。 我在这里关注微软教程。 我有两个上下文,一个叫做SchoolContext,用于保存所有与学校相关的模型,另一个上下文叫做ApplicationDbContext,用于Account模型。 这都被保存到sqlite数据库。 一切正常,直到我尝试将用户注册到我的上下文。 当我尝试注册我得到的用户时,无法找到AspNetUsers表错误。 如果我查看数据库,我看不到AspNetUser表。 我尝试添加迁移 ,但我仍然得到同样的错误。 为什么没有创建表? Startup.cs public class Startup { public IConfigurationRoot Configuration { get; } public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile(“appsettings.json”, optional: true, reloadOnChange: true) .AddJsonFile($”appsettings.{env.EnvironmentName}.json”, optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } // This method gets called by the runtime. Use this method […]