我的AddIdentityServices代码是
public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration config)
{
services.AddIdentityCore<AppUser>(opt =>
{
opt.Password.RequireDigit = false;
opt.Password.RequireNonAlphanumeric = false;
opt.Password.RequireLowercase = false;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<DataContext>()
.AddSignInManager<SignInManager<AppUser>>()
.AddDefaultTokenProviders();
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["TokenKey"]));
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(opt =>
{
opt.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = key,
ValidateIssuer = false,
ValidateAudience = false,
};
}).AddIdentityCookies();
services.AddAuthorization(opt =>
{
});
services.AddTransient<IAuthorizationHandler, IsHostRequirementHandler>();
services.AddScoped<TokenService>();
return services;
}
字符串
我的代币代码是
public string CreateToken(AppUser user)
{
var role_name = _userManager.GetRolesAsync(user).Result.FirstOrDefault().ToString();
var claim = new List<Claim> {
new Claim(ClaimTypes.Name, user.UserName),
new Claim(ClaimTypes.NameIdentifier, user.Id),
new Claim(ClaimTypes.Email, user.Email),
new Claim (ClaimTypes.Role,role_name.ToString())
};
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["TokenKey"]));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(claim),
Expires = DateAndTime.Now.AddDays(10),
SigningCredentials = creds,
};
var tokenHandler = new JwtSecurityTokenHandler();
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
型
控制器中的授权代码为
[Authorize(Roles = "Admin")]
[HttpPut, Route("setAction")]
public async Task<IActionResult> setAction(RequestVehicle requestVehicle)
{
return ...
}
型
这段代码是好的,在我的本地系统(windows 10),管理员用户可以调用setAction函数,一切都很好。
但是但是当发布我的项目在服务器上(windows server 202,IIS 10.0.2)这个函数说错误:405 Method Not Allowed
的数据
为什么IIS会出现405错误?
我在声明new Claim (ClaimTypes.Role,role_name.ToString())
中添加了角色名称,我不知道这是否正确
1条答案
按热度按时间voase2hg1#
这个配置问题解决了
字符串