我想将 JWT 身份验证用于我的 Web api,并将 cookie 身份验证用于 Razor 页面。我对控制器使用策略授权。在我的以下配置中,一切都适用于我的 Razor 页面Startup.cs:
services.AddIdentity<User, Role>(opt =>{
opt.Password.RequireDigit = false;
opt.Password.RequiredLength = 4;
opt.Password.RequireNonAlphanumeric = false;
opt.Password.RequireUppercase = false;
opt.Password.RequireLowercase = false;
})
.AddEntityFrameworkStores<DataContext>()
.AddRoleValidator<RoleValidator<Role>>()
.AddRoleManager<RoleManager<Role>>()
.AddSignInManager<SignInManager<User>>();
Run Code Online (Sandbox Code Playgroud)
但是我的 Controller 端点不起作用,当我使用下面的端点时,它可以工作:
IdentityBuilder builder = services.AddIdentityCore<User>(opt =>
{
opt.Password.RequireDigit = false;
opt.Password.RequiredLength = 4;
opt.Password.RequireNonAlphanumeric = false;
opt.Password.RequireUppercase = false;
opt.Password.RequireLowercase = false;
});
Run Code Online (Sandbox Code Playgroud)
但是角色没有被添加到用户声明中,因此我的 Razor 页面的授权策略属性总是返回Access Denied。
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public …Run Code Online (Sandbox Code Playgroud) crypto.js:74
this._handle.update(data, encoding);
^
TypeError: Data must be a string or a buffer
at TypeError (native)
at Hash.update (crypto.js:74:16)
at sha1 (/mnt/projects/Nodejs/develda/node_modules/mysql2/lib/auth_41.js:30:8)
at Object.token [as calculateToken] (/mnt/projects/Nodejs/develda/node_modules/mysql2/lib/auth_41.js:64:16)
at new HandshakeResponse (/mnt/projects/Nodejs/develda/node_modules/mysql2/lib/packets/handshake_response.js:25:24)
at ClientHandshake.sendCredentials (/mnt/projects/Nodejs/develda/node_modules/mysql2/lib/commands/client_handshake.js:46:27)
at ClientHandshake.handshakeInit (/mnt/projects/Nodejs/develda/node_modules/mysql2/lib/commands/client_handshake.js:122:10)
at ClientHandshake.Command.execute (/mnt/projects/Nodejs/develda/node_modules/mysql2/lib/commands/command.js:39:20)
at Connection.handlePacket (/mnt/projects/Nodejs/develda/node_modules/mysql2/lib/connection.js:417:28)
at PacketParser.onPacket (/mnt/projects/Nodejs/develda/node_modules/mysql2/lib/connection.js:93:16)
at PacketParser.executeStart (/mnt/projects/Nodejs/develda/node_modules/mysql2/lib/packet_parser.js:73:14)
at Socket.<anonymous> (/mnt/projects/Nodejs/develda/node_modules/mysql2 /lib/connection.js:101:29)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:547:20)
Run Code Online (Sandbox Code Playgroud)
当我尝试使用 node_modules/.bin/sequelize db:migrate 进行迁移时出现此错误
我认为 mysql2 包导致此错误
我很感激你的帮助