5

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.

kubi
  • 897
  • 2
  • 9
  • 23

1 Answers1

-1

Here is my startup.cs

public class Startup
{
    public IConfiguration Configuration { get; private set; }

    public Startup(IHostingEnvironment env)
    {
        var config = new ConfigurationBuilder()
            .AddJsonFile("config.json", optional: true)
            .SetBasePath(env.ContentRootPath)
            .Build();

        Configuration = config;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.ConfigureDataContext(Configuration);

        services.AddIdentity<ApplicationUser, IdentityRole>(p =>
        {
            p.Password.RequireDigit = false;
            p.Password.RequireLowercase = false;
            p.Password.RequireUppercase = false;
            p.Password.RequireNonAlphanumeric = false;
        })
        .AddEntityFrameworkStores<MyShuttleContext>()
        .AddDefaultTokenProviders();

        services.ConfigureDependencies();
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseStaticFiles();
        app.UseAuthentication();
        app.ConfigureRoutes();
    }
}

And here is my config.json

{
  "DefaultUsername": "carrier",
  "DefaultPassword": "******",
  "Data": {
    "UseInMemoryStore": "false",
    "DefaultConnection": {
      "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=MyShuttle;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
  },
  "EntityFramework": {
    "MyShuttleContext": {
      "ConnectionStringKey": "Data:DefaultConnection:ConnectionString"
    }
  }
}

My Controller code is same as yours. I'm not having any problems with this code. You could try checking your SQL Server Object Explorer to check whether the tables are correctly populated or not.

SOUPTIK BANERJEE
  • 143
  • 3
  • 10