OnAuthorization不呼叫我的PUT和后呼叫

时间:2019-01-23 12:15:14

标签: c# .net-core asp.net-core-mvc asp.net-core-webapi

我有以下用于身份验证的代码,它可以很好地在get调用isValid.Result true上正常工作,但是它不能调用我的控制器的PUT和POST调用,我可以在自己的post上放置调试器并进行调用。但是它没有调用我的post和put函数吗?

  public class HMAuthentication : Attribute, IAuthorizationFilter
    {
        private static Dictionary<string, string> allowedApps = new Dictionary<string, string>();
        private readonly UInt64 requestMaxAgeInSeconds = 300;  //5 mins
        private readonly string authenticationScheme = "aa";
        public void OnAuthorization(AuthorizationFilterContext context)
        {
  var isValid = isValidRequest(requests, APPId, incomingBase64Signature, nonce, requestTimeStamp);

                if (isValid.Result)
                {
                    var currentPrincipal = new GenericPrincipal(new GenericIdentity(APPId), null);                   
                }
}
}

细心拨打电话

 [Route("api/[controller]")]
    [ApiController]
    public class InfoController : ControllerBase
    {
        private IUserService _info;

        public InfoController (IUserService info)
        {
            _info= info;
        }
     [HttpGet("GetInfo/{Id}")]
                public async Task<UserBase> GetInfo(int Id)
                {
                    return await _info.GetInfo(Id);
                }

PUT CALL

     [HttpPut]
            [Route("UpdateParent")]
            public async Task<int>  UpdateParent([FromBody] parent parentInfo)
            {
                return await _info.UpdateParent(parentInfo);
            }
}

Startup.cs

 services.AddMvc(options => options.Filters.Add(new HMAuthentication ())).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

1 个答案:

答案 0 :(得分:0)

我认为问题可能出在[Route("UpdateParent")]属性上。但是如果没有routeConfig,我不能说太多。在路由错误的情况下,您可以使用的快速解决方案是为此请求添加显式路由,然后查看身份验证是否正常工作。这是一个示例:

将其添加到AppStart文件夹下的routeConfig.cs文件中:

routes.MapRoute(
    name: "UpdateParentHandler",
    url: "api/InfoController/UpdateParent",
    defaults: new { controller = "InfoController", action = "UpdateParent" }
);

PS:HardCoding路由不是一个好习惯。但是,在解决问题时,可以将其用作临时解决方案!干杯:)