Asp.Net web service: I would like to return error 403 forbidden

后端 未结 9 757
轻奢々
轻奢々 2020-12-01 13:38

I have got a web service programmed in c# / asp.net.

[WebService(Namespace = \"http://example.com/\")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProf         


        
相关标签:
9条回答
  • 2020-12-01 14:12

    The return Forbid(); creates a ForbidResult (Status403Forbidden by default).

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.forbid?view=aspnetcore-3.1

    0 讨论(0)
  • 2020-12-01 14:17
    Context.Response.StatusCode = 403;
    
    0 讨论(0)
  • 2020-12-01 14:19

    Forbidden 403 would be a result of access to forbidden content on your website. I think what you want here is to return a message as part of your Result that is "User is not logged on"

    0 讨论(0)
  • Your web service requests will first encounter your global.asax file. You can check & return there.

    0 讨论(0)
  • 2020-12-01 14:25

    You don't need to set both Context.Response.Status and Context.Response.StatusCode. Simply setting

    Context.Response.StatusCode = (int)System.Net.HttpStatusCode.Forbidden
    

    will automatically set Response.Status for you.

    0 讨论(0)
  • 2020-12-01 14:25

    You can protect all your methods by placing the code in your WebService constructor. This prevents your WebMethod from even being called:

    public Service(): base()
    {
        if (!GetUser().LoggedIn)
        {
            Context.Response.StatusCode = (int)System.Net.HttpStatusCode.Forbidden;
            Context.Response.End();
        }
    }
    
    0 讨论(0)
提交回复
热议问题