身份核心 - 更改密钥类型后无法登录



我尝试在我的应用程序中设置身份。我创建了 Razor 页面项目,并添加了新脚手架项目 ->标识。然后出现了新的文件夹 - 包含所有与身份相关的内容的区域。 我想更改的一件事是更改用户的主键。我想要 int 而不是 Guid。我关注了这个网站上的许多教程和帖子,但有些问题。我可以注册新用户,但无法登录。如果我尝试登录,我会被重定向到主页,但我仍然看到登录链接而不是注销,当然所有标有[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);
}
}

IdentityHostingStartup

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();
});
}
}

我已经将所有出现的IdentityUser都改成了ApplicationUser

您正在实现IdentityContextApplicationDbContext,两者都继承自IdentityDbContext并使用自定义的标识实体。很难说你提供的代码,但我最好的猜测是你正在用一个创建用户,并尝试用另一个登录。您只需要其中一个上下文,而不需要两个上下文。删除一个,然后确保所有内容都使用相同的上下文。

最新更新