本文介绍了身份3 SignInManager.PasswordSignInAsync()不返回任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Identity 3.0创建Web应用程序,并且SignInManager PasswordSignInAsync()方法有问题.我就像在文档中一样使用它,但是它不返回任何内容(应用程序代码仅在此处停止)这是我的控制器代码:

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

startup.cs文件中的配置:

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

感谢您的帮助

推荐答案

'PasswordSignInAsync()'方法不能采用'model.Email'作为第一个参数,它可以使用用户名来检查用户.以下是方法:

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) 



如果要检查电子邮件,可以使用 SignInAsync()方法,但这是在检查 CheckPasswordAsync()之后是真的这是您可能会做的:



if you want to go through checking the email you can use the SignInAsync() method but that is after checking if the CheckPasswordAsync() is truehere 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");
     }
}


但是由于 SignInAsync()不支持该参数,现在您将无法检查lockoutOnFailure,要检查它,您必须制作另一个显式方法


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

这篇关于身份3 SignInManager.PasswordSignInAsync()不返回任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:38