Asp.net core 标识核心-更改密钥类型后无法登录
我尝试在应用程序中设置标识。我创建了Razor Pages项目,并添加了新的脚手架项目->标识。然后出现了新的文件夹区域,其中包含所有与身份相关的内容。 我想更改的一件事是更改用户的主键。我想要int而不是Guid。我在这个网站上看了很多教程和帖子,但有些地方不对劲。我可以注册新用户,但无法登录。如果我尝试登录,我会被重定向到主页,但我仍然会看到登录链接而不是注销,当然,所有标有Asp.net core 标识核心-更改密钥类型后无法登录,asp.net-core,.net-core,asp.net-core-identity,Asp.net Core,.net Core,Asp.net Core Identity,我尝试在应用程序中设置标识。我创建了Razor Pages项目,并添加了新的脚手架项目->标识。然后出现了新的文件夹区域,其中包含所有与身份相关的内容。 我想更改的一件事是更改用户的主键。我想要int而不是Guid。我在这个网站上看了很多教程和帖子,但有些地方不对劲。我可以注册新用户,但无法登录。如果我尝试登录,我会被重定向到主页,但我仍然会看到登录链接而不是注销,当然,所有标有[Authorize]的视图对我来说都是不可访问的。 我将展示我更改的内容,我相信你们中的一位会注意到我丢失的一段代码
[Authorize]
的视图对我来说都是不可访问的。
我将展示我更改的内容,我相信你们中的一位会注意到我丢失的一段代码
识别上下文
public class ApplicationRole : IdentityRole<int> { }
public class ApplicationUserRole : IdentityUserRole<int> { }
public class ApplicationUser : IdentityUser<int> { }
public class IdentityContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
public IdentityContext(DbContextOptions<IdentityContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
我已经将所有出现的
IdentityUser
更改为ApplicationUser
您正在实现一个IdentityContext
和ApplicationDbContext
,它们都继承自IdentityDbContext
,并使用自定义的身份实体。使用您提供的代码很难说,但我最好的猜测是您正在使用一个创建用户,并尝试使用另一个登录。您只需要这些上下文中的一个,而不是两个。删除一个,然后确保所有内容都使用相同的上下文。你说得对。一个上下文是不必要的,所以我删除了它,但这个问题仍然存在
public class IdentityHostingStartup : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) => {
services.AddDbContext<IdentityContext>(options =>
options.UseSqlServer(
context.Configuration.GetConnectionString("IdentityContextConnection")));
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<IdentityContext>();
});
}
}
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.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}