Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identity 3 SignInManager.PasswordSignInAsync() doesn't return any result

I am creating web application with Identity 3.0 and have problems with SignInManager PasswordSignInAsync() method. I'm using it just like in documentation, but it doesn't return anything ( application code just stop there ) Here`s my controller code:

 public class AppController : Controller
{
    private IAccountService _service;
    private readonly SignInManager<User> _signInManager;
    private UserManager<User> _userManager;

    public AppController(IAccountService service, SignInManager<User> signInManager, UserManager<User> userManager)
    {
        _service = service;
        _signInManager = signInManager;
        _userManager = userManager;
    }

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Login(LoginViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = await _userManager.FindByEmailAsync(model.Email);
            var password = await _userManager.CheckPasswordAsync(user, model.Password);

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

            if (result.Succeeded)
            {
                return RedirectToAction(nameof(EmployeeController.Contact), "Employee");
            }
            if (result.IsLockedOut)
            {
                return View("Lockout");
            }
            if(result.IsNotAllowed)
            {
                return View("Not Allowed");
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                return View(model);
            }
        }
        return View(model);
    }
}

And configuration in startup.cs file:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddCaching();
        services.AddSession(options => {
            options.IdleTimeout = TimeSpan.FromMinutes(30);
            options.CookieName = ".MyApplication";
        });

        services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration["Data:DbContextConnection"]));


        services.AddIdentity<User, UserRole>(config => 
            {
                config.User.RequireUniqueEmail = true;
                config.Password.RequiredLength = 8;
                config.Cookies.ApplicationCookie.LoginPath = "/App/Login";
                config.SignIn.RequireConfirmedEmail = false;
                config.SignIn.RequireConfirmedPhoneNumber = false;
            })
        .AddEntityFrameworkStores<ApplicationDbContext,string>()
        .AddDefaultTokenProviders();

        services.AddTransient<IAccountService, AccountService>();
    }

public void Configure(IApplicationBuilder app)
    {
        app.UseStaticFiles();

        app.UseSession();

        app.UseIdentity();
        app.UseMvc(config =>
        {
            config.MapRoute(
                name: "Default",
                template: "{controller}/{action}/{id?}",
                defaults: new { controller = "App", action = "Index" }
                );
        });
    }

Thanks for any help

like image 528
PaulJ Avatar asked Apr 03 '16 09:04

PaulJ


2 Answers

The 'PasswordSignInAsync()' method cant take 'model.Email' as the first argument, it can check up for the user using his username.Here is the method:

 public virtual Task<SignInStatus> PasswordSignInAsync(
    string userName,
    string password,
    bool isPersistent,
    bool shouldLockout) 



if you want to go through checking the email you can use the SignInAsync() method but that is after checking if the CheckPasswordAsync() is true here is what you could possibly make:

var user = await _userManager.FindByEmailAsync(model.Email);
var password = await _userManager.CheckPasswordAsync(user, model.Password);

if(password)
{
   var result = await _signInManager.SignInAsync(
                    model.Email,
                    model.Password,
                    model.RememberMe);
     if (result.Succeeded)
     {
          return RedirectToAction(nameof(EmployeeController.Contact), "Employee");
     }
}


But now you wont be able to check the lockoutOnFailure since the SignInAsync() dont support this argument,to check it you have to make another explicit method

like image 171
hdrdiab Avatar answered Sep 18 '22 18:09

hdrdiab


var user = await userManager.FindByEmailAsync(model.Email);
var result = await signInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, false);
like image 38
Inaam Avatar answered Sep 22 '22 18:09

Inaam