Context.User.Identity.IsAuthenticated 始终经过身份验证吗?
我正在尝试创建一个 httphandler 来拦截我们网站上的示例 pdf 文件。 httphandler 在我的开发机器甚至我本地发布的网站中工作正常,如果我只是尝试连接到测试 url: https://test.com/admin/_/sample_reports/sample.pdf我将被发送到无效的访问页面。
因此,当我尝试访问它提供 PDF 文档的 URL 时,将其推送到我们的 IIS6 计算机。 context.User.Identity.IsAuthenticated 始终显示为 true。
我正在使用表单身份验证。下面是我用作处理程序的代码。
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>
I am trying to create a httphandler which will intercept a sample pdf file which we have in our website. The httphandler works fine from within my development machine and even my locally published website that if I just try to connect to the test url:
https://test.com/admin/_/sample_reports/sample.pdf I will get sent to the invalid access page.
So pushing it to our IIS6 machine when I try to go to the URL it serves up the PDF document. context.User.Identity.IsAuthenticated is always showing as true.
I'm using forms authentication. below is the code I am using as the handler.
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");
}}
in web.config I have the following for form authentication:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当身份验证 cookie 仍然设置并且对于表单身份验证仍然有效(未过期)时,
Context.User.Identity.IsAuthenticated
属性设置为true
。所以,你的 auth cookie 还活着;它可能是由调用其中之一引起的
FormsAuthentication
方法,如RedirectFromLoginPage
< /a> 或SetAuthCookie
正在设置身份验证 cookie;或者只是被遗忘的cookie。另外,最好使用
HttpRequest.IsAuthenticated
而不是您的示例中的Context.User.Identity.IsAuthenticated
。它检查HttpContext.User
和HttpContext.User.Identity
是否不null
和HttpContext.User.Identity.IsAuthenticated
> 属性设置为true
。在您的情况下,例如,当HttpContext.User
为null
时,您的代码将抛出NullReferenceException
。Context.User.Identity.IsAuthenticated
property is set totrue
when authentication cookie is still set and is still valid (not expired) for forms authentication.So, your auth cookie is still alive; it may be caused by calling one of those
FormsAuthentication
methods likeRedirectFromLoginPage
orSetAuthCookie
which are setting the auth cookie; or just by forgotten cookie.Also it would be better to use
HttpRequest.IsAuthenticated
instead ofContext.User.Identity.IsAuthenticated
for your example. It checks whetherHttpContext.User
andHttpContext.User.Identity
is notnull
andHttpContext.User.Identity.IsAuthenticated
property is set totrue
. In your case when e.g.HttpContext.User
isnull
your code will throwNullReferenceException
.你确定吗
此 .PDF 请求可能已由 IIS 6 静态文件处理程序(而不是 IIS 6 上的 HTTP 处理程序)处理。
Are you sure that
This .PDF request may have already been processed by IIS 6 static file handler instead of your HTTP handler on IIS 6.
您需要使用 ProcessRequest
编辑:那么罪魁祸首可能是IIS,您有以下设置吗?在 IIS 中,所有使用表单身份验证的应用程序都启用匿名访问
You need to use ProcessRequest
EDIT: Might be IIS that is the culprit then, do you have the following set? In IIS, anonymous access is enabled for all applications that use forms authentication