Asp.net Context.User.Identity.IsAuthenticated是否始终经过身份验证?
我正在尝试创建一个httphandler,它将截取我们网站上的一个示例pdf文件。httphandler在我的开发机器中工作良好,甚至在我本地发布的网站上,如果我只是尝试连接到测试url: 我将被发送到无效访问页面 所以,当我试图转到提供PDF文档的URL时,将它推到我们的IIS6机器上。context.User.Identity.IsAuthenticated始终显示为true 我正在使用表单身份验证。下面是我用作处理程序的代码Asp.net Context.User.Identity.IsAuthenticated是否始终经过身份验证?,asp.net,iis,forms-authentication,Asp.net,Iis,Forms Authentication,我正在尝试创建一个httphandler,它将截取我们网站上的一个示例pdf文件。httphandler在我的开发机器中工作良好,甚至在我本地发布的网站上,如果我只是尝试连接到测试url: 我将被发送到无效访问页面 所以,当我试图转到提供PDF文档的URL时,将它推到我们的IIS6机器上。context.User.Identity.IsAuthenticated始终显示为true 我正在使用表单身份验证。下面是我用作处理程序的代码 public void ProcessRequest(HttpC
public void ProcessRequest(HttpContext context)
{
if (context.User.Identity.IsAuthenticated)
{
string SampleURL = context.Request.AppRelativeCurrentExecutionFilePath;
context.Response.Buffer = true;
context.Response.Clear();
using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(SampleURL),FileMode.Open))
{
int length = (int)fs.Length;
byte[] buffer;
using (BinaryReader br = new BinaryReader(fs))
{
buffer = br.ReadBytes(length);
}
context.Response.Clear();
context.Response.Buffer = true;
context.Response.ContentType = "application/pdf";
context.Response.BinaryWrite(buffer);
context.Response.End();
}
}
else
{
context.Response.Redirect(
"~/Error/invalid_access.aspx");
}}
在web.config中,我有以下表单身份验证:
<authentication mode="Forms">
<forms name="Sample.Web" loginUrl="~/Security/" defaultUrl="~/default.aspx" protection="All" timeout="60" path="/" requireSSL="false" slidingExpiration="true" enableCrossAppRedirects="false" cookieless="UseDeviceProfile" domain="">
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
您需要使用
编辑:可能是IIS造成的,那么您有以下设置吗?在IIS中,所有使用表单身份验证的应用程序都启用了匿名访问
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
您需要使用
编辑:可能是IIS造成的,那么您有以下设置吗?在IIS中,所有使用表单身份验证的应用程序都启用了匿名访问
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
当身份验证cookie仍然设置并且对于表单身份验证仍然有效(未过期)时,Context.User.Identity.IsAuthenticated属性设置为
true
在表单验证的情况下,
表单身份验证模块使用
加密的身份验证票证
包含在身份验证cookie中
验证用户的身份。一旦有了
完成此操作后,它将替换
通用实体
in
Context.User.Identity
FormsIdentity
返回的对象
true
从其中进行身份验证
财产
因此,您的auth cookie仍然存在;这可能是由于调用了一些方法,如或正在设置auth cookie;或者只是被遗忘的饼干
另外,对于您的示例,最好使用而不是
Context.User.Identity.IsAuthenticated
。它检查HttpContext.User
和HttpContext.User.Identity
是否为null
和HttpContext.User.Identity.IsAuthenticated
属性是否设置为true
。在您的情况下,例如HttpContext.User
为null
您的代码将抛出NullReferenceException
Context.User.Identity.IsAuthenticated
属性设置为true
,此时身份验证cookie仍然设置且表单身份验证仍然有效(未过期)
在表单验证的情况下,
表单身份验证模块使用
加密的身份验证票证
包含在身份验证cookie中
验证用户的身份。一旦有了
完成此操作后,它将替换
通用实体
in
Context.User.Identity
FormsIdentity
返回的对象
true
从其中进行身份验证
财产
因此,您的auth cookie仍然存在;这可能是由于调用了一些方法,如或正在设置auth cookie;或者只是被遗忘的饼干
另外,对于您的示例,最好使用而不是Context.User.Identity.IsAuthenticated
。它检查HttpContext.User
和HttpContext.User.Identity
是否为null
和HttpContext.User.Identity.IsAuthenticated
属性是否设置为true
。在您的情况下,例如HttpContext.User
为null
时,您的代码将抛出NullReferenceException
您确定
所以,当我试图转到提供PDF文档的URL时,将它推到我们的IIS6机器上context.User.Identity.IsAuthenticated始终显示为true。
此.PDF请求可能已由IIS 6静态文件处理程序而不是IIS 6上的HTTP处理程序处理 你确定吗
所以,当我试图转到提供PDF文档的URL时,将它推到我们的IIS6机器上context.User.Identity.IsAuthenticated始终显示为true。
此.PDF请求可能已由IIS 6静态文件处理程序而不是IIS 6上的HTTP处理程序处理 当我发布代码时,ProcessRequest被截断,更正了帖子。请参阅更新到原始帖子,但启用了yes anonymous并拒绝用户=“?”当我发布代码时,ProcessRequest被截断,更正了帖子。请参阅更新到原始帖子,但启用了yes anonymous并拒绝用户=“?”我会检查这个但我不相信我会检查这个但我不相信