ASP .NET Identity 404 error
Hello. I need to use asp .net identity in API project and in IdentityServer4 project, but if I add asp .net identity to my API project i get 404 error when try to get access to athorized api resource. Without asp .net identity in API project all works well.
My code of IdentityServer4 Startup.cs
This config works well.
services.AddDbContext<DataContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc();
services.AddIdentity<CustomUser, IdentityRole>(x =>
{
x.Password.RequireDigit = false;
x.Password.RequiredLength = 1;
x.Password.RequireLowercase = false;
x.Password.RequireNonAlphanumeric = false;
x.Password.RequireUppercase = false;
x.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<DataContext>()
.AddDefaultTokenProviders();
services.AddIdentityServer(options =>
{
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;
})
.AddDeveloperSigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources()) //check below
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddAspNetIdentity<CustomUser>()
.AddProfileService<ProfileService>();
services.AddAutoMapper();
services.AddTransient<IProfileService, ProfileService>();
services.AddTransient<IUserService, UserService>();
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseIdentityServer();
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}
Startup.cs code of my API project
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
services.AddDbContext<DataContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("Baza.Today")));
services.AddCors(o => o.AddPolicy("DefaultPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}));
//services.AddIdentity<CustomUser, IdentityRole>(x =>
//{
// x.Password.RequireDigit = false;
// x.Password.RequiredLength = 1;
// x.Password.RequireLowercase = false;
// x.Password.RequireNonAlphanumeric = false;
// x.Password.RequireUppercase = false;
// x.User.RequireUniqueEmail = true;
//})
//.AddEntityFrameworkStores<DataContext>();
//.AddDefaultTokenProviders();
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:65112";
options.RequireHttpsMetadata = false;
options.ApiName = "api";
});
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseResponseCompression();
app.UseCors("DefaultPolicy");
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc();
}
if remove initialization of asp .net identity from Startup.cs of API project, it works well and i have access to authorized api methods by generated token, but if uncomment
//services.AddIdentity<CustomUser, IdentityRole>(x =>
//{
// x.Password.RequireDigit = false;
// x.Password.RequiredLength = 1;
// x.Password.RequireLowercase = false;
// x.Password.RequireNonAlphanumeric = false;
// x.Password.RequireUppercase = false;
// x.User.RequireUniqueEmail = true;
//})
//.AddEntityFrameworkStores<DataContext>();
//.AddDefaultTokenProviders();
it throws 404 error when i try to get access to authorized method of api by token. Where is the problem? How to use asp .net identity in API and IdentityServer project together?
WOW!
services.AddIdentity<CustomUser, IdentityRole>(x =>
{
x.Password.RequireDigit = false;
x.Password.RequiredLength = 1;
x.Password.RequireLowercase = false;
x.Password.RequireNonAlphanumeric = false;
x.Password.RequireUppercase = false;
x.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<DataContext>()
.AddDefaultTokenProviders();
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:65112";
options.RequireHttpsMetadata = false;
options.ApiName = "api";
});
I added
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
This config solved my problem, but why?
I don't know why the JwtBearerDefault assignments solved your problem.
I had the same problem as you (my project is using Asp.Net Core 2.1), and the problems seem to be due to the way that Asp.Net Core Identity is imported in 2.1 vs 2.0. They used to just scaffold all of the views, but that was all replaced with a blackbox import. You can still scaffold those objects, but they're behind an Area "/Identity", so the IdentityServer4 links still look to root for an object that is instead behind "Identity."
In addition, by default, since the Identity Ui is not scaffolded, the objects don't actually exist locally.
To solve these problems takes two steps:
- Update the Identity middleware to add DefaultUi.
services.AddIdentity<IdentityUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders() .AddDefaultUI(); - Add a mapping that essentially removes "/Identity" from the route. (Credit to UnknownQuestions from this thread)
services.AddMvc() .AddRazorPagesOptions(o => o.Conventions.AddAreaFolderRouteModelConvention("Identity", "/Account/", model => { foreach (var selector in model.Selectors) { var attributeRouteModel = selector.AttributeRouteModel; attributeRouteModel.Order = -1; attributeRouteModel.Template = attributeRouteModel.Template.Remove(0, "Identity".Length); } })) .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
This seems to be a general question about ASP.NET Core - not a bug report or an issue.
Please use one of the our free or commercial support options
See here for more details.
Thanks!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.