Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET Core 3.0中AspNetUserToken和定制JWT令牌之间的差异_C#_Asp.net Core_Authentication_Jwt_Asp.net Core Identity - Fatal编程技术网

C# .NET Core 3.0中AspNetUserToken和定制JWT令牌之间的差异

C# .NET Core 3.0中AspNetUserToken和定制JWT令牌之间的差异,c#,asp.net-core,authentication,jwt,asp.net-core-identity,C#,Asp.net Core,Authentication,Jwt,Asp.net Core Identity,我正在开发一个小型的项目API,我想实现JWT身份验证。我已经用JWT制作了一些API,并且总是定制实现和验证 这一次,我想使用身份/实体框架来加快速度,并使用已经为我制作的工具 在使用GenerateToken方法和浏览internet时,我注意到许多由IdentityFramework创建的表没有被使用。我对AspNetUserToken感兴趣 我注意到这一点,而不是 private object GenerateToken(IdentityUser user) { var toke

我正在开发一个小型的项目API,我想实现JWT身份验证。我已经用JWT制作了一些API,并且总是定制实现和验证

这一次,我想使用身份/实体框架来加快速度,并使用已经为我制作的工具

在使用GenerateToken方法和浏览internet时,我注意到许多由IdentityFramework创建的表没有被使用。我对
AspNetUserToken
感兴趣

我注意到这一点,而不是

private object GenerateToken(IdentityUser user)
{
    var tokenHandler = new JwtSecurityTokenHandler();
    var key = Encoding.UTF8.GetBytes(ApiConfig.JwtSecretKey);

    var tokenDescriptor = new SecurityTokenDescriptor
    {
        Subject = new ClaimsIdentity(new Claim[]
        {
            new Claim(ClaimTypes.Name, user.UserName),
            new Claim(ClaimTypes.Email, user.Email),
        }),
        Expires = DateTime.UtcNow.AddSeconds(double.Parse(ApiConfig.JwtExp)), //TODO: Try parse
        SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature),
        Audience = ApiConfig.JwtAudience,
        Issuer = ApiConfig.JwtIssuer
    };
    var token = tokenHandler.CreateToken(tokenDescriptor);
        
    return tokenHandler.WriteToken(token);
}
我用它来生成一个新的JWT令牌,添加声明、发行人、受众等。。。可能会被以下内容取代:

//Removes existing token
_userManager.RemoveAuthenticationTokenAsync(user, "lboard", "login"); 
//Creates a new one
var newToken = await _userManager.GenerateUserTokenAsync(user, "lboard", "login");
//Set the new token for the user 
await _userManager.SetAuthenticationTokenAsync(user, "lboard", "login", newToken);

我想知道这两种方法的区别是什么,如果使用自定义实现有任何好处,或者如果我更喜欢IdentityFramework实现。
GenerateUserTokenAsync
方法由其他
UserManager
方法内部使用,如
generatechangeMailTokenAsync
GenerateChangePhoneNumberTokenAsync

为了使用更抽象的
GenerateUserTokenAsync
,必须提供实际生成令牌的令牌提供程序。由于JWT访问令牌没有任何默认令牌提供程序,因此您仍然需要自己编写逻辑并注册自定义令牌提供程序,然后可以使用
GenerateUserTokenAsync
方法

您仍然需要自己编写JWT逻辑,包括声明等,但会增加开销