0

I am testing my Login() method in postman, and It always return false.

There is a user in my db with email: [email protected], username: [email protected], password: 111111.

When I use CheckPasswordAsync for this user, I get true.

When I try to pass object user instead of username in parameter I get false too.

My post method

        [HttpPost]
        public async Task<IActionResult> Login()//[FromBody]UserModel model)
        {
            var result = await _signInManager.PasswordSignInAsync
                ("[email protected]", "111111", false, false);

            //var user = await _userManager.FindByEmailAsync("[email protected]"); 
            //var result1 = await _userManager.CheckPasswordAsync  
            //    (user, "111111");

            //var result = await _signInManager.PasswordSignInAsync
            //     (user, "111111", false, false);
            if (result.Succeeded == true)
            {
                return Ok(result);
            }
            else
            {
                return BadRequest(result);
            }
        }

In postman I always get

{
    "succeeded": false,
    "isLockedOut": false,
    "isNotAllowed": true,
    "requiresTwoFactor": false
}

Code from my Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddDbContext<ApplicationContext>();
            services.AddIdentity<User, IdentityRole>(opts =>
            {
                opts.SignIn.RequireConfirmedAccount = true;
                opts.Password.RequiredLength = 6; 
                opts.Password.RequireNonAlphanumeric = false;  
                opts.Password.RequireLowercase = false;
                opts.Password.RequireUppercase = false; 
                opts.Password.RequireDigit = false; 
            })
                .AddEntityFrameworkStores<ApplicationContext>();
            services.AddTransient<IUnitOfWork, EFUnitOfWork>();
            services.AddTransient<ITestService, TestService>();
            services.AddTransient<IQuestionService, QuestionService>();
            services.AddTransient<IAnswerService, AnswerService>();
            services.AddTransient<IUserService, UserService>();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
8
  • Can you please provide the configuration code? As the response says, that action is not allowed, so it should be something related to those configurations and not the Identity implementation in ASP. May 2, 2020 at 20:23
  • @TonyTroeff I have edit my post
    – Roomey
    May 2, 2020 at 20:26
  • So, the first hing I have noticed is that part of the code where a confirmation is required for every account in order to sign in. Is this condition fulfilled? May 2, 2020 at 20:31
  • @TonyTroeff I don’t really understand what you are talking about. What part are you talking about?
    – Roomey
    May 2, 2020 at 20:33
  • 1
    That line of code: opts.SignIn.RequireConfirmedAccount = true; May 2, 2020 at 20:34

1 Answer 1

1

That line of code is what breaks your code:

opts.SignIn.RequireConfirmedAccount = true;

This configuration will not allow you to log in with an unconfirmed account which I think this one is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.