2

我使用Identity 3.0創建Web應用程序,並且SignInManager PasswordSignInAsync()方法出現問題。我使用它,就像在文檔中,但它不返回任何東西(應用程序代碼只是停在那裏) Here`s我控制器代碼:標識3 SignInManager.PasswordSignInAsync()不返回任何結果

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); 
    } 
} 

和配置startup.cs文件:

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" } 
       ); 
     }); 
    } 

感謝您的幫助

回答

7

'PasswordSignInAsync()'方法不能把'model.Email'作爲第一個參數, 它可以檢查了使用他的username.Here用戶的方法:

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



,如果你想通過檢查郵件,你可以使用SignInAsync()方法但那是後檢查是否CheckPasswordAsync()是真的 這裏是你可能作出:

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"); 
    } 
} 


但現在你不會b e能夠檢查lockoutOnFailure,因爲SignInAsync()不支持這個參數,爲了檢查它你必須做出另一個明確的方法