ASP.Net Core 3.1脚手架式身份页面未全部使用(即登录页面OK,注册页面NOK)..为什么



我刚刚用dotnet aspnet-codegenerator identity工具成功地构建了标识页面,并在启动时添加了一些自定义代码来填充一些角色。

然后我想用一些代码更新/Areas/Identity/Pages/Register.cshtml.cs文件,为新创建的用户添加自定义角色。。。但是这个页面中的断点没有到达。

  • 当我修改Login.cshtml的HTML时,没关系,我看到了.cs文件中的更改和断点已到达

  • 当我尝试在Register.cs.HTML中做同样的事情时,没有考虑任何内容,自定义HTML不会更新,断点也永远不会到达

也许我错过了启动会议中的一些内容?


编辑

如果我将Register.cshml/.cs重命名为Registry2,我可以访问路径/Identity/Account/Register

[Identity/Ancount/Register 2但是当以Register2为目标时,就达到了断点,并且HTML是最新的。。。

也许IdentityHostingStartup有隐藏的链接?


这里是:

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options =>
{
options.SignIn.RequireConfirmedAccount = true;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();

services.AddAuthentication();
services.AddControllersWithViews();
services.AddMvc();
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RoleManager<IdentityRole> roleManager, UserManager<IdentityUser> userManager)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
});
}
}

非常感谢朋友^^

下面是一个工作演示,演示如何向新创建的用户添加角色:

对于Startup.cs,无需DI UserManager和RoleManager:

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{           
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddAuthentication();
services.AddControllersWithViews();
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();           
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}

对于Register.cshtml.cs:

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
if (ModelState.IsValid)
{
bool x = await _roleManager.RoleExistsAsync("Admin");
if (!x)
{
// first we create Admin role   
var role = new IdentityRole();
role.Name = "Admin";
await _roleManager.CreateAsync(role);
}
var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email };
var result = await _userManager.CreateAsync(user, Input.Password);
var data = await _userManager.AddToRoleAsync(user, "Admin");
if (result.Succeeded)
{                   
//...                                    
}
}
return Page();
}

相关内容

  • 没有找到相关文章

最新更新