ASP.NET 表单身份验证 - 何时使用?

发布于 2024-10-11 06:19:53 字数 123 浏览 3 评论 0原文

我无法决定是否应该进行表单身份验证?表单身份验证通常用于个性化,即为已知用户定制内容。我没有这样的要求。我在数据库中有用户名和密码,需要根据数据库对用户进行身份验证吗?在 ASP.NET 2.0 中实现相同目标的最佳推荐实践是什么?

I am not able to decide whether I should go for Forms Authentication ? Forms authentication is often used for personalization, where content is customized for a known user. I do not have such requirement. I have usernames and passwords in the DB and need to authenticate the users against the DB ? What is the best recommended practice to achieve the same in ASP.NET 2.0 ?

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

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

发布评论

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

评论(2

中二柚 2024-10-18 06:19:53

Forms Authentication 用于身份验证,您可以使用会员信息来自定义页面,但不是必须这样做。

Forms Authentication is used for authentication, you can use the membership information to customize pages, but you don't have to.

潇烟暮雨 2024-10-18 06:19:53

长话短说:
FormsAuthenication 类与密码无关。您需要使用它来保持用户登录到您的网站。为了验证密码,请使用 Membership 类或您自己的自定义系统。


FormsAuthentication 类的文档中:

表单身份验证使用户能够
Web 的密码验证
不需要的应用程序
Windows 身份验证。有表格
认证时,用户信息为
存储在外部数据源中,
例如会员数据库,或者
配置文件
应用。一旦用户
已验证,表单验证
维护一个身份验证票证
cookie 或 URL 中,以便
经过身份验证的用户不需要
为每个请求提供凭据。

如果查看 FormsAuthentication 的类定义,您将看到 验证方法。文档说“根据存储在应用程序配置文件中的凭据验证用户名和密码。您不想使用此,因为您想存储用户和密码在数据库中,而不是配置文件中。这是整个 FormsAuthentication 类中唯一与密码相关的方法。

发出表单票证的正确方法是使用 SetAuthCookie 方法,该方法不需要密码。那么如何/在哪里检查密码呢?

答案:会员资格(或自定义的内容)。

ASP.NET 2.0 版成员资格
功能提供安全凭证
应用程序用户的存储。它还
提供会员 API
简化验证用户的任务
与表单一起使用时的凭据
验证。会员提供者
抽象底层存储用于
维护用户凭据。

会员资格是一个很大的主题,您确实需要花一些时间研究它并编写测试应用程序。我至少阅读了 关于 ASP.NET 的成员资格、角色和配置文件的多部分系列

读完本文后,您可能会想是否应该编写自己的自定义会员资格提供程序。您可能想看看这里的一些答案,以获取有关 stackoverflow 或在出现问题时开始一个新问题。

TL;DR:
The FormsAuthenication class has nothing to do with passwords. You need to use it to keep users logged in to your website. In order to validate the passwords, use the Membership class or your own custom system.


From the documentation for the FormsAuthentication class:

Forms authentication enables user and
password validation for Web
applications that do not require
Windows authentication. With forms
authentication, user information is
stored in an external data source,
such as a Membership database, or in
the configuration file for an
application. Once a user is
authenticated, forms authentication
maintains an authentication ticket in
a cookie or in the URL so that an
authenticated user does not need to
supply credentials with each request.

If you look at the class definition for FormsAuthentication, you'll see an Authenticate method. The documentation says "Validates a user name and password against credentials stored in the configuration file for an application." You don't want to use this because you want to store users and passwords in a database, not the config file. This is the ONLY method related to passwords in the whole FormsAuthentication class.

The correct way to issue a forms ticket is with the SetAuthCookie method, which does not take a password. So how/where do you check the password?

Answer: Membership (or something custom).

The ASP.NET version 2.0 membership
feature provides secure credential
storage for application users. It also
provides a membership API that
simplifies the task of validating user
credentials when used with forms
authentication. Membership providers
abstract the underlying store used to
maintain user credentials.

Membership is a large topic and you really need to spend some time researching it and writing a test application. I'd read at least the first three articles in the Multipart Series on ASP.NET's Membership, Roles, and Profile.

After you're done reading that, you're probably going to wonder if you should write your own custom Membership provider. You might want to take a look at some of the answers here on SO for guidance on that stackoverflow or start a new question when that comes up.

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