【问题标题】:OnAuthorization not calling my PUT and post callOnAuthorization 不调用我的 PUT 和后调用
【发布时间】:2019-06-17 00:45:53
【问题描述】:

我有下面的身份验证代码,它在获取调用isValid.Result true 获取调用工作正常但它不调用我的控制器 PUT 和 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);

【问题讨论】:

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


    【解决方案1】:

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

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

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

    PS:硬编码路线不是好的做法。但是,您可以在解决问题时将其用作临时修复!干杯:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      相关资源
      最近更新 更多