Asp.Net Web 服务:我想返回错误 403 禁止

发布于 2024-11-01 01:20:09 字数 664 浏览 5 评论 0原文

我有一个用 c# / asp.net 编程的网络服务。

[WebService(Namespace = "http://example.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
[System.ComponentModel.ToolboxItem(false)]
public class Service: System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Result GetData()
    {
        User user = GetUser();

        if (user.LoggedIn)
        {
            return GetData();
        }
        else
        {
            // raise exception -> return error 403
        }
    }

如何从该 Web 服务返回错误 403?我可以抛出异常 - 但这显示了异常而不是他的错误。

有什么想法吗?

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

[WebService(Namespace = "http://example.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
[System.ComponentModel.ToolboxItem(false)]
public class Service: System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Result GetData()
    {
        User user = GetUser();

        if (user.LoggedIn)
        {
            return GetData();
        }
        else
        {
            // raise exception -> return error 403
        }
    }

How is it possible to return error 403 out of this web service? I can throw an exception - but this shows the exeption and not his error.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

思念满溢 2024-11-08 01:20:09

如果您使用 MVC,您将执行以下操作:

            return new HttpStatusCodeResult(HttpStatusCode.Forbidden);

If you were using MVC you'd do the following:

            return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
方圜几里 2024-11-08 01:20:09

您无需同时设置 Context.Response.StatusContext.Response.StatusCode。只需设置

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

即可自动为您设置Response.Status

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.

许仙没带伞 2024-11-08 01:20:09

为了完全回答这个问题 - 这是我使用的代码(感谢 strider 提供更多信息):

[WebService(Namespace = "http://example.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
[System.ComponentModel.ToolboxItem(false)]
public class Service: System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Result GetData()
    {
        User user = GetUser();

        if (user.LoggedIn)
        {
            return GetData();
        }
        else
        {
            Context.Response.Status = "403 Forbidden"; 
            //the next line is untested - thanks to strider for this line
            Context.Response.StatusCode = 403;
            //the next line can result in a ThreadAbortException
            //Context.Response.End(); 
            Context.ApplicationInstance.CompleteRequest(); 
            return null;
        }
    }

To answer the question completely - this is the code I've used (thank you strider for more information):

[WebService(Namespace = "http://example.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
[System.ComponentModel.ToolboxItem(false)]
public class Service: System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Result GetData()
    {
        User user = GetUser();

        if (user.LoggedIn)
        {
            return GetData();
        }
        else
        {
            Context.Response.Status = "403 Forbidden"; 
            //the next line is untested - thanks to strider for this line
            Context.Response.StatusCode = 403;
            //the next line can result in a ThreadAbortException
            //Context.Response.End(); 
            Context.ApplicationInstance.CompleteRequest(); 
            return null;
        }
    }
千里故人稀 2024-11-08 01:20:09

您可以通过将代码放置在 WebService 构造函数中来保护所有方法。这甚至会阻止您的 WebMethod 被调用:

public Service(): base()
{
    if (!GetUser().LoggedIn)
    {
        Context.Response.StatusCode = (int)System.Net.HttpStatusCode.Forbidden;
        Context.Response.End();
    }
}

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();
    }
}
残花月 2024-11-08 01:20:09

在 Asp.Net Web Api 2 中,您可以使用:

return new StatusCodeResult(HttpStatusCode.Forbidden, this);

In Asp.Net Web Api 2, you'd use:

return new StatusCodeResult(HttpStatusCode.Forbidden, this);
阳光下的泡沫是彩色的 2024-11-08 01:20:09
Context.Response.StatusCode = 403;
Context.Response.StatusCode = 403;
浪推晚风 2024-11-08 01:20:09

您的 Web 服务请求将首先遇到您的 global.asax 文件。您可以检查&返回那里。

Your web service requests will first encounter your global.asax file. You can check & return there.

无敌元气妹 2024-11-08 01:20:09

aspnet core 你可以返回forbidResult。这是一个 IResult。

return new ForbidResult();

aspnet core you can return forbidResult. This is an IResult.

return new ForbidResult();
清晨说晚安 2024-11-08 01:20:09

Forbidden 403 是由于访问您网站上的禁止内容而导致的。我认为您在这里想要的是返回一条消息作为结果的一部分,即“用户未登录”

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"

抱猫软卧 2024-11-08 01:20:09

return Forbid(); 创建一个 ForbidResult(默认情况下为 Status403Forbidden)。

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文