隐藏 asp.net WebMethod 返回的异常的堆栈跟踪?

发布于 2024-07-18 06:52:30 字数 413 浏览 5 评论 0原文

我在我的 aspx 页面中使用带有属性 [WebMethod] 的方法。 我不使用任何 asp.net ajax,而是使用 jQuery 来调用这些方法并返回 JSON 中的对象。 这一切都很好。

接下来,我在 webMethod 内添加了授权检查,如果当前用户无权访问该功能,我需要让调用 JavaScript 知道。

因此,我抛出一个 AccessViolationException 异常,然后可以通过 JavaScript 中的 OnError 回调函数对其进行解析。 这也有效,但例外包括完整的 StackTrace,我不想将其提供给调用客户端。

当 WebMethod 返回业务对象时,我还可以使用哪些其他方法向客户端返回“访问被拒绝”?

我正在使用 ASP.Net 3.5SP1 和 jQuery 1.32

I am using methods with the Attribute [WebMethod] in my aspx pages. I don't use any asp.net ajax but jQuery to call these methods and return objects in JSON. This all works fine.

Next I added an authorization check inside the webMethod, if the current user doesn't have access to the feature I need to let the calling JavaScript know.

So I am throwing an AccessViolationException exception which can then be parsed by the OnError callback function in JavaScript. This works too but the exception includes the full StackTrace and I don't want to make this available to the calling client.

What other ways I could use to return an "Access Denied" to the client when the WebMethod returns a business object?

I'm using ASP.Net 3.5SP1 and jQuery 1.32

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

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

发布评论

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

评论(5

甜嗑 2024-07-25 06:52:30

您还可以在 web.config 中添加:,

customErrors mode="On"/

这将删除堆栈跟踪并只留下异常消息

You can also add a:

customErrors mode="On"/

in your web.config, this will cut away the stack trace and leave you only the exception message

奢欲 2024-07-25 06:52:30

为什么通过线路传播错误? 为什么不使用错误响应?

只需将您的对象包装在响应对象中,该对象可以包含状态错误代码和要呈现给用户的错误消息。

Why propagate errors through the wire? why not use an error response ?

Just wrap your object in a response object wich can contain an error code for status and an error message to present to users.

梦中楼上月下 2024-07-25 06:52:30

正如 NunFur 所建议的,我改变了我的方法,而不是抛出错误,而是返回一个“更丰富”的对象。

至少有两个选择,第一个是将我的业务对象封装到具有一些状态属性的响应对象中。 我尝试过这个,但它使 JSON 变得更加复杂。
因此,我没有添加新对象,而是向业务对象添加了两个属性,例如 ServiceStatus 和 ServiceMessage。 默认情况下,这些是 200 和 '',但如果出现任何问题(无法访问,正确错误),可以通过 WebMethod 代码进行设置。 在这种情况下,他们的业务对象将为“空”(没有数据)。 然后,JavaScript 代码首先检查 ServiceStatus 并做出适当反应。

我将这两个字段添加到 WebMethods 返回的所有对象中,甚至是一个简单的字符串。 他们必须实现具有这两个属性的接口。

现在,我可以完全控制网络上的情况,以防发生意外情况。

感谢您的输入

As suggested by NunFur I changed my approach and rather than throwing an error, I return a 'richer' object.

There are at least two options, the first one would be to encapsulate my business object into a response object with some status properties. I tried this but it makes the JSON more complicated.
So rather than adding a new object I added two properties to my business object, something like ServiceStatus and ServiceMessage. By default these are 200 and '', but can be set by the WebMethod code if anything goes wrong (no access, proper error). In this case they business object will be 'empty' (no data). The JavaScript code then first checks for the ServiceStatus and reacts appropriately.

I add the two fields to all my objects that are returned by WebMethods, even a simple string. They have to implement an Interface with those two properties.

Now I have complete control over that goes over the wire in case something unexpected is happening.

Thanks for the input

玩心态 2024-07-25 06:52:30

我会在事情真正出错时保留例外情况。 (例如,无法连接到数据库)

要么不返回任何内容(null/nill/whatever),要么返回 false bool 值。

抱歉,我没有比这更好的答案......我必须继续寻找自己。

I save exceptions for when things go really wrong. (e.g. can't connect to the database)

Either return nothing (null/nill/whatever), or return a false bool value.

Sorry that I don't have a better answer than that...I'll have to keep looking myself.

情深缘浅 2024-07-25 06:52:30

您可以查看 SoapException: http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapexception(VS.71).aspx

我只是不确定它是否会工作从 JavaScript 调用。 特别是如果它是通过 get-request 调用的。

顺便说一句,据我所知,当应用程序访问它无法访问的内存时,会抛出 AccessViolationException 。

/阿斯格

You could look at SoapException: http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapexception(VS.71).aspx

I'm just not sure, if it will work when it is called from JavaScript. Espeially if it's called with a get-request.

BTW AccessViolationException is to my best knowlegde ment to be thrown when the application is accessing memory it has no access to.

/Asger

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