C# Request.IsAuthenticated 如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2201238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:20:31  来源:igfitidea点击:

How does Request.IsAuthenticated work?

c#asp.nethttprequest

提问by MetaGuru

MSDN Code Sample Description: The following code example uses the IsAuthenticated property to determine whether the current request has been authenticated. If it has not been authenticated, the request is redirected to another page where users can enter their credentials into the Web application. This is a common technique used in the default page for an application.

MSDN 代码示例说明:以下代码示例使用 IsAuthenticated 属性来确定当前请求是否已通过身份验证。如果尚未通过身份验证,请求将重定向到另一个页面,用户可以在该页面将其凭据输入到 Web 应用程序中。这是应用程序默认页面中常用的技术。

This is great but no detail or anything...

这很棒,但没有细节或任何东西......

What exactly is it checking for? How do I set it to true?

它究竟在检查什么?我如何将其设置为true?

Go the extra mile: Where would I find more detailed documentation about this?

加倍努力:我在哪里可以找到关于此的更详细的文档?

采纳答案by Oliver

Thanks to Google, I found a cached version of the post @keyboardP refers to in his answer. I'm posting that answer/post here as a reference for others since the original link is broken (2012-12-06).

感谢谷歌,我找到了@keyboardP 在他的回答中引用的帖子的缓存版本。由于原始链接已损坏(2012-12-06),我将在此处发布该答案/帖子作为其他人的参考。

Original questionthat the answer below refers to:

以下答案所指的原始问题

I have a forms based application that is giving me fits. I noticed that, in a location where the IsAuthenticated property had been True, it was now false and the was not working as expected. I am wondering if I have a setting that is invalid??

我有一个基于表单的应用程序,这让我很合适。我注意到,在 IsAuthenticated 属性为 True 的位置,它现在为 false 并且未按预期工作。我想知道我是否有一个无效的设置?

Can anyone tell me what sets the IsAuthenticated property to True--what constitues logging in.

谁能告诉我是什么将 IsAuthenticated 属性设置为 True——什么构成了登录。

Answer by Daniel Kent:

丹尼尔·肯特的回答:

Request.IsAuthenticatedis not just for forms authentciation - it is valid no matter what type of authentication is being used (Windows, Passport, Forms or our own custom scheme)

Request.IsAuthenticated不仅用于表单身份验证 - 无论使用哪种类型的身份验证(Windows、Passport、Forms 或我们自己的自定义方案),它都是有效的

HttpRequest.IsAuthenticatedwill be true when the user making the request has been authenticated. Essentially, this property provides the same information as Context.User.Identity.IsAuthenticated.

HttpRequest.IsAuthenticated当发出请求的用户已通过身份验证时为真。本质上,此属性提供与 相同的信息Context.User.Identity.IsAuthenticated

At the start of a request, Context.User.Idenitycontains a GenericIdentitywith a null username. The IsAuthenticatedproperty for this object will return falseso Request.IsAuthenticatedwill be false. When an authentication module handles the Application_AuthenticateRequestevent and successfuly authenticates the user it replaces the GenericIdentityin Context.User.Identitywith a new IIdentityobject that will return truefrom its IsAuthenticatedproperty. Request.IsAuthenticatedwill then return true.

在请求开始时,Context.User.Idenity包含GenericIdentity具有空用户名的 。IsAuthenticated此对象的属性将返回,false因此Request.IsAuthenticated将是false。当身份验证模块处理Application_AuthenticateRequest事件并成功验证用户时,它将用一个新对象替换GenericIdentityin Context.User.Identity,该IIdentity对象true将从其IsAuthenticated属性返回。Request.IsAuthenticated然后将返回true

In the case of Forms authentication, the forms authentication module uses the encrypted authentication ticket contained in the authentication cookie to authenticate the user. Once it has done this, it replaces the GenericIdentityin Context.User.Identitywith a FormsIdentityobject that returns Truefrom its IsAuthenticatedproperty.

在表单身份验证的情况下,表单身份验证模块使用身份验证 cookie 中包含的加密身份验证票来对用户进行身份验证。完成此操作后,它会将GenericIdentityin替换为 从其属性返回Context.User.IdentityFormsIdentity对象。TrueIsAuthenticated

So, setting IsAuthenticatedto trueis actually different to logging in. As Jeff says, logging in to forms authentication happens when the authentication ticket is generated and sent to the client as a cookie. (RedirectFromLoginPageor SetAuthCookie) What we are talking about with IsAuthenticatedis authentication that happens with each page request. Logging in happens when a user enters their credentials and is issued a ticket, authentication happens with each request.

因此,设置IsAuthenticatedtrue在实际上是不同的伐木。杰夫说,到时产生的身份验证票和发送到客户端的cookie的窗体身份验证登录会发生。(RedirectFromLoginPageSetAuthCookie) 我们正在谈论的 IsAuthenticated是与每个页面请求一起发生的身份验证。当用户输入他们的凭据并获得票证时,就会进行登录,每次请求都会进行身份验证。

回答by keyboardP

There's a quite detailed post by Daniel Kent here. (Snippet)

Daniel Kent这里有一篇非常详细的帖子。(片段)

Request.IsAuthenticated is not just for forms authentciation - it is valid no matter what type of authentication is being used (Windows, Passport, Forms or our own custom scheme)

HttpRequest.IsAuthenticated will be true when the user making the request has been authenticated. Essentially, this property provides the same information as Context.User.Identity.IsAuthenticated.

Request.IsAuthenticated 不仅用于表单身份验证 - 无论使用何种类型的身份验证(Windows、Passport、Forms 或我们自己的自定义方案),它都是有效的

当发出请求的用户已通过身份验证时,HttpRequest.IsAuthenticated 将为 true。本质上,此属性提供与 Context.User.Identity.IsAuthenticated 相同的信息。