在IIS上授权说405方法不允许

x8diyxa7  于 2023-11-19  发布在  其他
关注(0)|答案(1)|浏览(134)

我的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())中添加了角色名称,我不知道这是否正确

voase2hg

voase2hg1#

这个配置问题解决了

<?xml version="1.0" encoding="utf-8"?>
<configuration>

    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        
        <modules runAllManagedModulesForAllRequests="true" >
            <remove name="WebDAVModule" />
        </modules>
        <handlers>
            <remove name="WebDAV" />
            <remove name="WebDAVModule" />

            <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
            <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
    </system.webServer>
</configuration>

字符串

相关问题