c#/ aspx错误处理 [英] c#/aspx error handling

查看:52
本文介绍了c#/ aspx错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写aspx登录系统,并且登录过程需要对用户名和密码进行

有效性检查。


i被告知提出异常是昂贵的,但我想要一个自定义错误

消息显示取决于发生了什么错误(让我们说:无效

用户名,密码无效,密码必须包含5个或更多字符,

等)。


目前,我的登录方法返回true / false取决于是否

登录成功与否。但是,如果登录失败,我的

调用函数将如何知道发生了什么错误而没有引发异常?


i知道我可以从登录方法返回数字回到调用

函数(其中每个数字代表一个不同的错误),但后来我会

必须检查每个使用<的函数的数值br />
登录方法,并在每次调用

函数时写下appropritate错误消息。


有没有办法可以写我的代码所以我只需要在

登录函数中写错误,如果

登录失败,调用函数会收到错误信息吗?


感谢

i am trying to write aspx login system and the login process requires a
validity check of username and password.

i have been told that raising exception is costly, but i want a custom error
message to be displayed depending on what error occurred (let''s say: invalid
username, invalid password, password must contain 5 or more characters,
etc).

at the moment, my login method is returning true/false depending on whether
the login was succesful or not. however, if the login fails, how will my
calling function know what error occurred without raising an exception?

i know i could return numbers from the login method back to the calling
function (where each number represents a different error), but then i would
have to check the value of the number on every calling function that uses
the login method, and write the appropritate error messages on EVERY calling
function.

is there a way i can write my code so i only have to write the error in the
login function, and calling functions will pick up the error message if the
login fails?

thanks

推荐答案

如果你想轻松一点,只需返回一个字符串而不是一个布尔值

值。 Null表示成功,其他任何内容都是实际的错误消息(或者如果要本地化,则为

资源ID)。但这不是一个好的安全实践

来显示这个信息,它允许更容易的磨合(说密码

不正确意味着你有一个有效的登录名,现在你只需要

密码,提供密码需要缩短多长时间的信息

显着缩小搜索空间等等。如果您想要安全,只需

显示通用的无效的用户名或密码。登录失败的消息。


Jerry


" suzy" < me@nospam.com>在消息中写道

新闻:例如************** @ TK2MSFTNGP12.phx.gbl ...
If you want to go the easy way just return a string instead of a boolean
value. Null means success, anything else is the actual error message (or its
resource id if you want to localize). But it''s not a good security practice
to show this info, it allows for a lot easier break-in (saying the password
is incorrect means you have a valid login name and now you just need the
password, giving out information on how long the password needs to be narrow
down the search space dramatically and so on). If you want to be safe just
show a generic "Invalid user name or password" message for failed logins.

Jerry

"suzy" <me@nospam.com> wrote in message
news:eg**************@TK2MSFTNGP12.phx.gbl...
我正在写aspx登录系统和登录过程需要用户名和密码的有效性检查。

我被告知提高异常是昂贵的,但我想要一个自定义的
错误消息根据发生的错误显示(让我们说:
无效的用户名,密码无效,密码必须包含5个或更多字符,
等)。

目前,我的登录方法返回true / false取决于
登录是否成功。但是,如果登录失败,我的
调用函数将如何知道发生了什么错误而没有引发异常?

我知道我可以将登录方法中的数字返回给调用
function(每个数字表示不同的错误),但是i
必须检查每个使用登录方法的调用函数的数字值,并在每个函数上写下appropritate错误消息
调用函数。

有没有办法可以编写我的代码所以我只需要在
中写入错误登录函数,调用函数就会收到错误信息如果
登录失败?

感谢
i am trying to write aspx login system and the login process requires a
validity check of username and password.

i have been told that raising exception is costly, but i want a custom error message to be displayed depending on what error occurred (let''s say: invalid username, invalid password, password must contain 5 or more characters,
etc).

at the moment, my login method is returning true/false depending on whether the login was succesful or not. however, if the login fails, how will my
calling function know what error occurred without raising an exception?

i know i could return numbers from the login method back to the calling
function (where each number represents a different error), but then i would have to check the value of the number on every calling function that uses
the login method, and write the appropritate error messages on EVERY calling function.

is there a way i can write my code so i only have to write the error in the login function, and calling functions will pick up the error message if the login fails?

thanks



是的我看到你在说什么,但这是我感兴趣的理论。我只是用

使用登录作为例子。


如果我有一个功能怎么办?确实需要返回发生的错误类型

。我可以在成功时返回null并在

失败时返回错误消息,但是如果该函数是为了返回数据集或

那么该怎么办?如何在该场景中检索错误?


我能想到的唯一巧妙的做法是抛出一个异常,但

''事情的声音太贵了。

Jerry III < JE ****** @ hotmail.com>在消息中写道

新闻:eq ************** @ TK2MSFTNGP09.phx.gbl ...
Yes I see what you''re saying, but it''s the theory I''m interested in. I just
used the login as an example.

What if I had a function that really did need to return the type of error
that occurred. I could return null on success and the error message on
failure, but what if the function was meant to return a dataset or
something? How would I retrieve an error in that scenario?

The only neat way I can think of doing it is to throw an exception, but
that''s too costly by the sounds of things.
"Jerry III" <je******@hotmail.com> wrote in message
news:eq**************@TK2MSFTNGP09.phx.gbl...
如果你想去简单的方法就是返回一个字符串而不是一个布尔值。 Null表示成功,其他任何内容都是实际的错误消息(如果要本地化,则为
其资源ID)。但是显示此信息并不是一个好的安全
做法,它允许更容易的闯入(说
密码不正确意味着你有一个有效的登录名,现在你只需要
密码,提供密码需要多长时间的信息
大幅缩小搜索空间等等)。如果您想要安全,只需显示通用的无效的用户名或密码。登录失败的消息。

Jerry

suzy < me@nospam.com>在消息中写道
新闻:例如************** @ TK2MSFTNGP12.phx.gbl ...
If you want to go the easy way just return a string instead of a boolean
value. Null means success, anything else is the actual error message (or its resource id if you want to localize). But it''s not a good security practice to show this info, it allows for a lot easier break-in (saying the password is incorrect means you have a valid login name and now you just need the
password, giving out information on how long the password needs to be narrow down the search space dramatically and so on). If you want to be safe just
show a generic "Invalid user name or password" message for failed logins.

Jerry

"suzy" <me@nospam.com> wrote in message
news:eg**************@TK2MSFTNGP12.phx.gbl...
我正在尝试编写aspx登录系统和登录过程需要用户名和密码的有效性检查。

我被告知提高异常是昂贵的,但我想要一个自定义的
i am trying to write aspx login system and the login process requires a
validity check of username and password.

i have been told that raising exception is costly, but i want a custom


错误

根据发生的错误显示的消息(让我们说:
message to be displayed depending on what error occurred (let''s say:


无效

用户名,密码无效,密码必须包含5个或更多字符,
等。目前,我的登录方法返回true / false取决于
username, invalid password, password must contain 5 or more characters,
etc).

at the moment, my login method is returning true/false depending on


是否

登录成功与否。但是,如果登录失败,我的调用函数
将如何知道发生了什么错误而没有引发异常?

我知道我可以将登录方法中的数字返回给调用
function(其中每个数字表示不同的错误),但是
the login was succesful or not. however, if the login fails, how will my calling function know what error occurred without raising an exception?

i know i could return numbers from the login method back to the calling
function (where each number represents a different error), but then i


必须检查
使用的每个调用函数的数字值登录方法,并在EVERY上编写appropritate错误消息
have to check the value of the number on every calling function that uses the login method, and write the appropritate error messages on EVERY


调用

函数。

有没有办法可以编写我的代码所以我只需要在
function.

is there a way i can write my code so i only have to write the error in

登录函数中编写错误,如果
login function, and calling functions will pick up the error message if


登录失败?

感谢
login fails?

thanks




您好!
我能想到的唯一巧妙的方式o这样做是为了抛出一个例外,但是由于事物的声音太贵了。
The only neat way I can think of doing it is to throw an exception, but
that''s too costly by the sounds of things.




您可以返回使用枚举值功能。这将保持

非常安全的东西,你可以很容易地处理返回值。

怎么样?


错误不一定是例外。如果您能够处理并预期错误,那么抛出异常通常不是一个好主意。投掷

例外会自动强制你的API用户围绕每个方法包裹一个

try / catch / finally块。


还是我错了? :-)


-

venlig hilsen /关心

anders borum

-



You could return use an enumeration value from the function. This would keep
things pretty typesafe and you could handle the return value pretty easily.
How about that?

An error is not necessarily an exception. If you can handle and anticipate
the error, it''s generally not a good idea to throw an exception. Throwing
exceptions automatically forces the users of your API to wrap a
try/catch/finally block around each method.

Or am I wrong? :-)

--
venlig hilsen / with regards
anders borum
--


这篇关于c#/ aspx错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆