简体   繁体   English

通过WCF生成文件,通过具有访问权限验证的ASP.NET下载

[英]Generate file through WCF, download through ASP.NET with access validation

I need to implement a operation in which the user requests a file which takes sometime to be generated ( 2 - 4 minutes). 我需要执行一项操作,其中用户请求一个文件,该文件需要花费一些时间(2-4分钟)才能生成。 After that, the user needs to download the file, preferentially through ASP.NET ( to make use of the browser download facility). 之后,用户需要优先通过ASP.NET下载文件(以利用浏览器下载工具)。 Only the specified user can download this file. 只有指定的用户可以下载此文件。

Here is how i tried to do it: 这是我尝试执行的操作:

First, I built a duplex wcf service. 首先,我建立了一个双工wcf服务。 The user calls it with the data it needs and then it starts the file generation, notifying the progress through the callback channel. 用户使用所需的数据对其进行调用,然后开始生成文件,并通过回调通道通知进度。 In the end, the service is supposed to send to the user a url, token which he will use to download the file. 最后,该服务应该发送给用户一个URL,令牌,他将使用该令牌来下载文件。 This part works fine. 这部分工作正常。

I saved the file to a temp folder in the asp_data folder, to prevent it from being accessed directly. 我将文件保存到asp_data文件夹中的temp文件夹中,以防止直接访问它。 Then I created a aspx page to receive the token ( whatever it is), validate it against the current user, the defined expiration, and replace the response with the file. 然后,我创建了一个aspx页面来接收令牌(无论令牌是什么),针对当前用户,定义的到期时间对其进行验证,然后将响应替换为文件。

Then the things got messy. 然后事情变得混乱了。 I do not know the right way to generate the token through WCF, return it to the client and use it the access the download page. 我不知道通过WCF生成令牌,将其返回给客户端并使用其访问下载页面的正确方法。 I tried two different approaches, but I think I'm giving up on both: 我尝试了两种不同的方法,但我想都放弃了:

  • Generate a guid for the file, encrypt it inside a FormsAuthenticationTicket (with the user information and expiration) and send it to the client. 生成文件的guid,在FormsAuthenticationTicket(带有用户信息和有效期)内对其进行加密,然后将其发送给客户端。 The client then uses the ticket encrypted string as the token to the download page, which validates the user in the ticket against the current one , check the expiration, extracts the guid and sends the file back. 然后,客户端使用票证加密的字符串作为下载页面的令牌,下载页将根据当前票证对票证中的用户进行验证,检查其到期时间,提取GUID并将文件发回。 The problem is that the generated encrypted string gets really big, unusable in a url. 问题是生成的加密字符串变得很大,无法在url中使用。

  • Generate a guid for the file, save it in ticket ( with validation data and the path to the file) in the httpcontext session. 生成文件的guid,并将其保存在httpcontext会话的票证中(包含验证数据和文件的路径)。 the wcf service then passes the guid to the client, who uses it to access the download page. 然后,wcf服务将guid传递给客户端,客户端使用它来访问下载页面。 The download page checks the session, retrieves the ticket, serves the file. 下载页面检查会话,检索票证,提供文件。 The problem is I'm having some trouble acessing the session in the WCF operation. 问题是我在WCF操作中无法访问会话。 The user requests the file, the server starts a thread to generate the file and make the callback calls, so the first server call returns (nothing). 用户请求文件,服务器启动一个线程来生成文件并进行回调调用,因此第一个服务器调用返回(什么都没有)。 When I've finish generating the file with success, the callback thread tries to access the session, save the ticket and return the guid to the user in a 'FinishOperationXXX' callback. 当我成功完成文件生成后,回调线程将尝试访问会话,保存故障单并在“ FinishOperationXXX”回调中将GUID返回给用户。 I cant access the session, though, because it seems to be no longer available to the callback thread. 但是,我无法访问该会话,因为它似乎不再可用于回调线程。

I don't want to use a database to do this, and I'm trying to avoid downloading the file throught the WCF itself, but I need to get this working. 我不想使用数据库来执行此操作,并且我试图避免通过WCF本身下载文件,但是我需要使此工作正常进行。 I guess I'll manage to do it somehow, but I wonder: 我想我会设法做到的,但是我想知道:

Am I doing this the hard way? 我是不是很难做到这一点? Do anyone have a clue about implementing something alike? 有没有人知道实现类似的东西?

Why do you need encryption and a FormsAuthenticationTicket? 为什么需要加密和FormsAuthenticationTicket?

Wouldn't it work well enough to just name the file with the type of file, user's userid and a timestamp (filetype_userid_timestamp.ext) and only allow users to download files that contain their userid in the middle field? 仅用文件的类型,用户的用户名和时间戳(filetype_userid_timestamp.ext)命名文件,仅允许用户下载在中间字段中包含其用户名的文件,这样做是否足够好?

(type of file being different for each page doing this in case you had more than one...) (如果您的页面不止一个,则每个页面的文件类型都会不同...)

Authentication for the user should already be handled by the session right? 用户的身份验证应该已经由会话处理了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM