Asp.Net Core-Identity SignInManager-如何使用附加条件登录,以便用户登录(例如:ClientId,UserName和Password))

问题描述 投票:0回答:1

在Asp.Net Core中,如何使用额外条件登录-Identity SignInManager

示例我想检查并验证ClientId,用于登录的用户名和密码。

类似这样的东西

var result = await SignInManager.PasswordSignInAsync(model.ClientId, model.Email, model.Password, model.RememberMe, shouldLockout: false);

请参见下面的完整代码

        public async Task<IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User logged in.");
                    return LocalRedirect(returnUrl);
                }
                if (result.RequiresTwoFactor)
                {
                    return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return RedirectToPage("./Lockout");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return Page();
                }
            }

            // If we got this far, something failed, redisplay form
            return Page();
        }```

c# asp.net-core asp.net-core-identity
1个回答
0
投票

我将通过编写在原始SignInManager.PasswordSignInAsync上实现该接口的接口和包装器类来建议使用adapter pattern方法。

此包装器类将具有一个方法,该方法将客户机ID与其余参数一起输入,执行验证,然后在完成后调用SignInManager.PasswordSignInAsync。

您的登录方法将通过startup.cs使用。netCore dependency injection通过接口调用包装器类。

public interface ISignInManagerAdapter
{
    Task<Microsoft.AspNetCore.Identity.SignInResult>  PasswordWithClientIdSignInAsync(string clientId, string userName, string password, bool isPersistent, bool lockoutOnFailure);
}

public class SignInManagerAdapter
{ 
    private readonly IClientValidatorService _clientValidatorService;
    public SignInManagerAdapter(IClientValidatorService clientValidatorService)
    {
       //you can add dependency injection interfaces to constructor parameter like below example.
       this._clientValidatorService = clientValidatorService;
    }
    public async Task<Microsoft.AspNetCore.Identity.SignInResult>  PasswordWithClientIdSignInAsync(string clientId, string userName, string password, bool isPersistent, bool lockoutOnFailure)
    {
        var validationStatus = _clientValidatorService.ValidateClientId(clientId);
        if (validationStatus == "Active")
        {
           var result = await SignInManager.PasswordSignInAsync(model.ClientId, model.Email, model.Password, model.RememberMe, shouldLockout: false);
           //do something
        }
        else
        {
           //do something
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.