- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试在 ASP.NET Core 2.0 中实现身份。我在解决这个问题时遇到了很多问题。
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Ctrack6_Custom"))
);
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
etc...
ApplicationUser.cs 使用 Guid 作为 key 。还可以在Role等中设置
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser<Guid>
{
[MaxLength(50)]
public string FirstName { get; set; }
[MaxLength(50)]
public string LastName { get; set; }
[MaxLength(5)]
public string OrgCode { get; set; }
public ApplicationUser() : base()
{
}
}
ApplicationDbContext.cs 在此文件中的类定义中引发错误。 ApplicationDbContext 抛出此错误:
The type 'App.Identity.ApplicationUser' cannot be used as type parameter 'TUser' in the generic type or method 'IdentityDbContext'. There is no implicit reference conversion from 'App.Identity.ApplicationUser' to 'Microsoft.AspNetCore.Identity.IdentityUser'.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
builder.Entity<blah>()
.HasKey(c => new { fields, for, key });
}
public DbSet<etc> Etcs {get; set; }
}
最佳答案
更改public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
至
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
您需要有一个与 ApplicationUser 类类似的 ApplicationRole 类。据我记得,一旦您指定了 key 类型(在本例中为 Guid,默认为字符串),即使您不使用角色,您也需要包含角色和 key 类型。
关于c# - 类型 ApplicationUser 不能用作泛型类型或方法 'TUser' 中的类型参数 'IdentityDbContext<TUser>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49788816/
我在 Visual Studio 中创建了一个新的 ASP.NET MVC 项目,模板创建了一个 IdentityConfig.cs 文件,如下所示 using System; using Syste
我有一个从同事那里得到的 ASP MVC 项目,它有 2 个上下文。一个来自登录所需的模板,另一个是自制的。所以我想把两者结合起来,但我总是收到这个错误: ApplicationUser is not
我正在使用 ASP.NET Identity 2.0。目前我想通过表 UserDetails 扩展 ApplicationUser(将 AspNetUsers 重命名为 Users),例如 First
是否可以检查 ApplicationUser 是否有声明? 我知道可以检查 ApplicationUser 是否在某个角色中 userManager.IsInRoleAsync(application
在我的 MVC 应用程序中,我使用 TPT inheritance 创建了基本 ASP Identity ApplicationUser 类的两个子类。 ,并希望向对象添加一些声明,以便我可以轻松地在
我正在尝试为我的模拟 ApplicationUserManager 提供一个 ApplicationUser。当我尝试添加角色时,我注意到 Roles 是一个只读属性。不确定在我的测试中实现的最佳方式
我创建了一个名为 Person 的自定义类来存储姓名、地址等。这个类/模型是从其他模型交叉引用的,包括 ApplicationUser: public class ApplicationUser :
我有以下实体: public class ApplicationUser : IdentityUser { ... public int? StudentId { get; set;
我在 Asp.net 中有一个 Web 应用程序,首先使用 Entity Framework 6 和数据库。用户连接时遇到问题。这是我的代码: Web.config
我正在从 Identity 1.0.0 迁移到 Identity 2.0.1 之后 article 并且生成的迁移代码与新的 IdentityUser 无关。它不会添加新列。 所以我做了一个新项目并再
我正在尝试掌握 ASP.NET MVC 5 中引入的新成员资格系统,但我遇到了一个小问题,我很确定您能帮我解决这个问题。 我要离开 this tutorial并为 ApplicationUser 引入
应该在什么基础上决定使用 IdentityDbContext与 IdentityDbContext在 ASP.NET MVC5 应用程序中? 使用IdentityDbContext给我们带来什么好处而
我希望能够使用 WebAPI 2 公开用户列表。但是,由于我在 MVC5 中使用新的 Asp.Net 身份验证框架,我似乎找不到一种方法来仅将特定字段标记为 数据成员。 这是我所拥有的: [DataC
我有这门课 public class ApplicationUser : IdentityUser { public string Email { get; set; } public s
Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IE
我正在谷歌搜索以在 ASP.NET Identity 3.0 的 Controller 中获取应用程序用户,但我只找到 5 及以下的结果。 我正在尝试获取 ApplicationUser 属性,但我根
我问过有关旧 MVC 版本和成员资格的类似问题,但这些解决方案不适用于 Idendity 和 MVC 6。 我想在 _LoginPartial.cshtml 文件中显示用户的名字。因此,我想访问当前登
我首先使用实体框架代码对用户创建小的 CRUD 操作。现在我需要使用存储过程删除用户,所以我找到许多链接获取如何在 EF 中使用存储过程的解决方案。所以我写这段代码那个。但是我在类中编写此代码和方
我将这个问题作为文档发布,因为我花了很多时间才找到这个简单的问题。我正在使用 VS15 生成的原始 MVC 项目并尝试对其进行修改。 错误:[InvalidOperationException:实体类
我正在使用 EF6/.Net 4.5.1 在 Web 窗体的用户控件中检索 ListView 的数据。我修改了 ApplicationUser 以包含一个使用 FK 属性的导航属性 [1:1],该属性
我是一名优秀的程序员,十分优秀!