The problem concernes Identity authentication in ASP.NET Core 2.0.
After PasswordSignInAsync() the result has a status of succeeded. So i'm redirecting to action Chat in StaticController.
[HttpPost("login")]
public async Task<ActionResult> Login([Bind] LoginModel lm) {
if (!ModelState.IsValid)
return BadRequest(ModelState);
var result = await _signInManager.PasswordSignInAsync(lm.Username, lm.Password, false, false);
if (result.Succeeded)
return RedirectToAction("Login", "Static");
else
return BadRequest(new { message = "Invalid username or password" });
}
StaticController.Chat()
[HttpGet("/chat")]
public ActionResult Chat() {
if (User.Identity.IsAuthenticated)
return File("~/chat/index.html", "text/html");
else
return RedirectToAction("Login", "Static");
}
and here the problem occurs because the User.Identity.IsAuthenticated is always false. Even if after performing Login() action i call from browser localhost:5000/chat it' still false.
These are related solutions that i've found, but none of them work:
This solution is outdated. I'm using ASP.NET Core 2.0 and UseCookieAuthentication() is obsolete.
User.Identity.IsAuthenticated always false after PasswordSignInAsync gives success
As it states in this solution User.Identity.IsAuthenticated returns false after SignInAsync invoked after Login() action i should call the Chat() method again (as i did by localhost:5000/chat) and it should work. Well the problem is it doesn't.
Here's my Startup.cs:
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<ApplicationDbContext>(options => {
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options => {
// Password settings
options.Password.RequireDigit = true;
options.Password.RequiredLength = 6;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
// User settings
options.User.RequireUniqueEmail = true;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseMvc();
app.UseAuthentication();
}
}
Please help.