ASP.NET核心身份

问题描述

如何使用UserManagerSignInManager调用现有的Login存储过程?是否可以使用现有的数据库结构,并想从存储过程中创建授权和身份验证?

但是当我根据自己的要求使用时,会出现错误

[Route("api/[controller]")]
[ApiController]
[AllowAnonymous]
public class AuthenticateController : ControllerBase
{
    private readonly UserManager<ApplicationUser> userManager;
    private readonly RoleManager<IdentityRole> roleManager;
    private readonly IConfiguration _configuration;
    private readonly IsqlApplicationUserRepository sqlApplicationUserRepository;

    public AuthenticateController(UserManager<ApplicationUser> userManager,RoleManager<IdentityRole> roleManager,IConfiguration configuration,IsqlApplicationUserRepository sqlApplicationUserRepository)
    {
        this.userManager = userManager;
        this.roleManager = roleManager;
        _configuration = configuration;
        this.sqlApplicationUserRepository = sqlApplicationUserRepository;
    }
    [HttpPost]
    [Route("login")]
    public async Task<IActionResult> Login(SiteUser model)
    {
    //this function i want to use instead of .FindByNameAsync

        var user1 = sqlApplicationUserRepository.GetUserProfile(model.UserName);//this function i want to use instead of .FindByNameAsync

        var user = await userManager.FindByNameAsync(model.UserName);

    //so what change i need to do in this section ir in signinmanager to authenticate my user from membership tables using store procedure. 
        if (user1 != null && await userManager.CheckPasswordAsync(user1,model.Password))
        {
            var userRoles = await userManager.GetRolesAsync(user1);

            var authClaims = new List<Claim>
            {
                new Claim(ClaimTypes.Name,user1.UserName),new Claim(JwtRegisteredClaimNames.Jti,Guid.NewGuid().ToString()),};

            foreach (var userRole in userRoles)
            {
                authClaims.Add(new Claim(ClaimTypes.Role,userRole));
            }

            var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:Secret"]));

            var token = new JwtSecurityToken(
                issuer: _configuration["JWT:Validissuer"],audience: _configuration["JWT:ValidAudience"],expires: DateTime.Now.AddHours(3),claims: authClaims,signingCredentials: new SigningCredentials(authSigningKey,SecurityAlgorithms.HmacSha256)
                );

            return Ok(new
            {
                token = new JwtSecurityTokenHandler().Writetoken(token),expiration = token.ValidTo
            });
        }

        return Unauthorized();
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)