下载文件时ASP.MVC会话丢失

时间:2021-03-18 03:49:16

I have a Download page with links to files stored locally on the web server. All is well when a user opens a text file, but when they open an Office file and choose "Open" from the resulting dialog in IE or Firefox, ASP creates a new session.

我有一个下载页面,其中包含指向本地存储在Web服务器上的文件的链接。当用户打开文本文件时,一切都很好,但是当他们打开Office文件并从IE或Firefox中生成的对话框中选择“打开”时,ASP会创建一个新会话。

My simplified controller method is:

我简化的控制器方法是:

<HttpGet()>
<AppAuthorize(Domain.Security.TransactionId.Download)>
Public Function Download(fileName As String) As ActionResult
    ' fileName example: C:\ProgramData\MyCompany\MyApp\SomeFile.txt
    Return New FilePathResult(fileName, "application/octet-stream")
End Function

Debugging shows that as soon as you click "Open" in the browser, the Global.asax Session_Start() event is triggered.

调试显示,只要在浏览器中单击“打开”,就会触发Global.asax Session_Start()事件。

下载文件时ASP.MVC会话丢失

If you Save the file and then view it in IE a new session is not created. It is only a problem if you click the Open button as pictured above.

如果保存文件,然后在IE中查看它,则不会创建新会话。如果您单击上面的图片中的“打开”按钮,则只会出现问题。

Why is this and how can I prevent it?

为什么这样,我该如何预防呢?

The core problem is that when my app sees a new session started and the user has a valid Forms Authentication cookie, it assumes IIS restarted and deletes the user's cookie. So every time a user opens a Word doc they get logged out of the app.

核心问题是,当我的应用程序看到新会话已启动并且用户具有有效的表单身份验证cookie时,它会假定IIS已重新启动并删除用户的cookie。因此,每次用户打开Word文档时,他们都会从应用程序中注销。

2 个解决方案

#1


2  

When a DOC link is clicked in Internet Explorer, it hands it off to Word. This means that Word does the downloading, not IE. Because of this, there is a new "browser connection" made and a new session will be created because it's not actually IE doing the downloading.

在Internet Explorer中单击DOC链接时,会将其移交给Word。这意味着Word会进行下载,而不是IE。因此,有一个新的“浏览器连接”,并且将创建一个新的会话,因为它实际上不是IE正在进行下载。

#2


2  

I'm answering my own question.

我正在回答我自己的问题。

I found a solution after Matt's answer pointed me in the right direction. I simply disabled SessionState for the controller by adding an attribute as follows:

在Matt的回答指出我正确的方向后,我找到了解决方案。我只是通过添加一个属性来为控制器禁用SessionState,如下所示:

<SessionState(SessionStateBehavior.Disabled)>
Public Class AttachmentController
    ...
End Class

Now Office can download files but not trigger a new session. My app is no longer broken.

现在Office可以下载文件但不会触发新会话。我的应用程序不再被破坏。

#1


2  

When a DOC link is clicked in Internet Explorer, it hands it off to Word. This means that Word does the downloading, not IE. Because of this, there is a new "browser connection" made and a new session will be created because it's not actually IE doing the downloading.

在Internet Explorer中单击DOC链接时,会将其移交给Word。这意味着Word会进行下载,而不是IE。因此,有一个新的“浏览器连接”,并且将创建一个新的会话,因为它实际上不是IE正在进行下载。

#2


2  

I'm answering my own question.

我正在回答我自己的问题。

I found a solution after Matt's answer pointed me in the right direction. I simply disabled SessionState for the controller by adding an attribute as follows:

在Matt的回答指出我正确的方向后,我找到了解决方案。我只是通过添加一个属性来为控制器禁用SessionState,如下所示:

<SessionState(SessionStateBehavior.Disabled)>
Public Class AttachmentController
    ...
End Class

Now Office can download files but not trigger a new session. My app is no longer broken.

现在Office可以下载文件但不会触发新会话。我的应用程序不再被破坏。