Search code examples
c#asp.netasp.net-mvcasp.net-identity

Why is the login attempt returning {NotAllowed} in the PasswordSignInAsync method (ASP.NET)


I used an identity in my ASP.NET project, it created the login and register feature automatically and i can register to the website just fine, after registering the account gets logged in and i can use all the features i made, but if i log out and try to log into the same account again the login process always fails. The accounts are being stored in the database as following https://i.imgur.com/M1EbfGj.png

I didn't change the code that was made by visual studio 2019 and after debugging all i know is that the password is passed correctly to the database but the result comes out as {NotAllowed}, the line where that happens is

var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
            

if someone wants to see the entire project it can be found in https://github.com/Alysty/TaskManager


Solution

  • File that is most likely your issue: TaskManager/TaskManager/Areas/Identity/IdentityHostingStartup.cs

    Location is in requiring the account to be confirmed in this line:

    services.AddDefaultIdentity<TaskManagerUser>(options => options.SignIn.RequireConfirmedAccount = true)
    

    My guess is that your account is not confirmed so you need to confirm it or remove this constraint.

    using System;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Identity.UI;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using TaskManager.Areas.Identity.Data;
    using TaskManager.Data;
    
    [assembly: HostingStartup(typeof(TaskManager.Areas.Identity.IdentityHostingStartup))]
    namespace TaskManager.Areas.Identity
    {
        public class IdentityHostingStartup : IHostingStartup
        {
            public void Configure(IWebHostBuilder builder)
            {
                builder.ConfigureServices((context, services) => {
                    services.AddDbContext<ApplicationDbContext>(options =>
                        options.UseSqlServer(
                            context.Configuration.GetConnectionString("ApplicationDbContextConnection")));
    
                    services.AddDefaultIdentity<TaskManagerUser>(options => options.SignIn.RequireConfirmedAccount = true)
                        .AddEntityFrameworkStores<ApplicationDbContext>();
                });
            }
        }
    }