Ich benutze Postman, um eine Anfrage an/api/account/login und Benutzer anmeldet, dann sende ich Anfrage an/api/account/logout und es meldet sich erfolgreich ab, aber wenn ich dies tue im Frontend, fülle ich das Login-Formular aus und sende die Anfrage, bekomme ich response.ok, aber wenn ich versuche, eine Anfrage an/api/account/logout zu senden, wirft 401 Unauthorized.Benutzer ist nicht nach erfolgreichem PasswordSignInAsync
Ich denke, es ist ein Problem in meinem Startup.cs ConfigureServices Methode, aber ich bin nicht sicher, was es
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddJsonOptions(x =>
x.SerializerSettings.ReferenceLoopHandling =
Newtonsoft.Json.ReferenceLoopHandling.Ignore);
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("RemoteConnection")));
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<AppDbContext, int>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options =>
{
options.Cookies.ApplicationCookie.AutomaticChallenge = false;
});
// services.AddScoped<UserGroupRepository, UserGroupRepository>();
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped<ISurveysRepository, SurveysRepository>();
services.AddScoped<IUsersRepository, UsersRepository>();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
HotModuleReplacement = true,
ReactHotModuleReplacement = true
});
}
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
}
ist mir aufgefallen, wenn Anfrage Senden Formular, dass auch hier der Benutzer nicht berechtigt ist,
[HttpPost("login")]
public async Task<IActionResult> Login([FromBody] LoginRequest request)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var result = await _signInManager.PasswordSignInAsync(request.UserName, request.Password, true, false);
if (!result.Succeeded)
{
return StatusCode((int)HttpStatusCode.Conflict, new ErrorResponse
{
ErrorMessage = "Invalid User Name or Password."
});
}
if (User.Identity.IsAuthenticated)
return NoContent();
else
return BadRequest(ModelState);
}
Und Ihre Configure mich vermisst Thod? Das ist der interessante Teil, da dort die Middleware registriert ist und die Reihenfolge der Registrierungen wichtig ist. – Tseng
Ich habe die Configure-Methode hinzugefügt – Redas