ASP.NET 表单身份验证的工作原理:从请求中识别 cookie

发布于 2024-12-27 17:37:12 字数 207 浏览 0 评论 0原文

我正在阅读 ASP.NET 中的表单身份验证,有些时候无法理解:

James 输入用户名密码,它们保存在数据库中。来自用户名的 cookie 被创建、加密并附加到响应中。据我了解,当我们收到请求时,我们需要识别收到的 cookie 是来自 James,这样我们就可以显示他的自定义页面。

我想了解的是系统如何从 cookie 中检索用户名,然后从数据库加载他的信息?

I am reading on form authentication in ASP.NET and cannot understand some moment:

James enters a username-password, they are saved in the db. A cookie from username is created, encrypted and attached to a response. As I understand then, when we get a request we need to recognise that cookie received are from James and so we can show his customised page.

What I would like to understand is how system will retrieve username form cookie and then load his info from db?

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

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

发布评论

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

评论(3

毁梦 2025-01-03 17:37:12

Forms Auth 与存储无关。它不必使用数据库,实际上您可以在 web.config

那么会发生什么情况是

  1. 用户登录。
  2. 用户根据成员资格提供程序(可以使用 SQL、Active DIRECTORY、web.config、Oracle、MySQL 等)进行身份验证。
  3. 为用户创建表单身份验证令牌,并将其放置在通过 cookie 访问用户机器。
  4. 每个后续请求都会读取表单身份验证令牌,并查询提供程序以获取用户详细信息。
  5. 用户详细信息用于填充 HttpContext 中的用户身份和请求的当前线程,然后可供您的代码使用。

在您的代码中,您可以检查用户 Page 类 (WebForms) 中的属性或 控制器中的 User 属性类(MVC)。

虽然您可以通过当前线程或当前上下文获取它,但不建议这样做,特别是一旦您开始使用后台任务,其中身份可能不会传播到线程,或者上下文可能会更改。

您会注意到,当用户登录时,数据库中不会存储任何内容。这一切都在表单身份验证令牌中,并且在每个请求上从其存储中检索用户的工作都已为您完成。

Forms Auth is storage agnostic. It doesn't have to use a database, indeed you can use it with usernames and passwords in web.config.

So what happens is

  1. A user logs in.
  2. The user is authenticated against the membership provider (which can use SQL, Active DIrectory, web.config, Oracle, MySQL, whatever)
  3. A forms authentication token is created for the user, and is placed on the user machine via a cookie.
  4. Each subsequent request reads the forms authentication token, and queries the provider to get the user details.
  5. The user details are used to populate the user identity in the HttpContext and current thread for the request which is then available to your code to use.

In your code you can check the User property in the Page class (WebForms) or the User property in the controller class (MVC).

While you can get at it via the current thread, or the current context it's not advised, especially once you start using background tasks, where the identity may not propagate to the thread, or the context may change.

You'll note that nothing is stored in a database when the user logs in. It's all in the forms authentication token, and the work of retrieving the user from it's store on each request is done for you.

只是一片海 2025-01-03 17:37:12

Afaik Forms Authentication 不会在任何数据库中存储或加载任何内容。您可以使用数据库来存储用户名和密码,也可以将它们放在 web.config 中。如何存储用户凭据并验证它们取决于您,并且可以与表单身份验证分开进行。

一旦您验证了用户(针对数据库或其他一些逻辑存储),您就可以使用 FormsAuthentication 来写入身份验证 cookie。您无需担心 cookie 的解密问题。

您可以从 System.Threading.Thread.CurrentPrincipal.Identity.Name 获取用户名。要从数据库检索用户信息,您可以使用主体身份名称的值查询数据库。

回复评论

是的,您可以将表单身份验证与成员资格提供商、活动目录或您自己的自定义用户数据库结合使用。 FormsAuth 根本不关心密码,除非它存储在 web.config 中(如blowdart 更完整的答案中所述)。它只是写入 cookie,该 cookie 被解密并用于自动创建线程标识。

其他信息

尽管这被标记为答案,但blowdart的响应要完整得多。如果您在 ASPX 页面或 MVC 控制器中需要它,您确实不应该从线程获取身份,请使用他引用的属性。

Afaik Forms Authentication does not store or load anything in any database. You can use a database to store the username and password, or you can put them in the web.config. How you store user credentials and validate them is up to you, and can happen separately from Forms Authentication.

Once you have validated a user (against database or some other logical storage), you use FormsAuthentication to write the authentication cookie. You do not need to worry about decrypting the cookie.

You can get the username from System.Threading.Thread.CurrentPrincipal.Identity.Name. To retrieve user's info from the database, you would query the database using the value if the principal identity name.

Response to comments

Right, you can use forms authentication with the membership provider, active directory, or your own custom user database. FormsAuth doesn't care about the password at all, unless it is stored in web.config (as noted in blowdart's more complete answer). It just writes the cookie, which is decrypted and used to create the thread identity automatically.

Additional Info

Even though this was marked as the answer, blowdart's response is much more complete. You really should not get the identity from the thread if you need it in an ASPX page or MVC controller, use the properties he referenced.

茶花眉 2025-01-03 17:37:12

您可以通过调用 User.Identity.Name 在 Web 表单中获取用户名,例如:

protected void Page_Load(object sender, EventArgs e)
{
    string userName = User.Identity.Name;
}

ASP.NET 会为您解释 cookie,您不必自己读取它。或者您的问题是如何在数据库中存储用户和密码?

You get the username in your web form by calling User.Identity.Name, e.g. like this:

protected void Page_Load(object sender, EventArgs e)
{
    string userName = User.Identity.Name;
}

ASP.NET interprets the cookie for you, you don't have to read it yourself. Or is your question how to store the user and password in the DB?

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