2

現在、以下のコードを使用して、古いASP.netページでネットワークサーバーからクライアントにファイルをダウンロードしています。

MVC3でアプリを書き直したので、この機能をアップグレードしたいと思います。web.configに次の行を書き込むことで、ネットワーク共有からファイルにアクセスできると主張する投稿をいくつか見ました。

<authentication mode="Windows"/>
<identity impersonate="true" userName="" password="" />

ただし、現在、フォーム認証モードを使用してサイトにログオンしています。それはログイン機能に干渉しますか?

これがダウンロードコードです。

Partial Class DownloadFile2
   Inherits System.Web.UI.Page

   Private BufferSize As Integer = 32 * 1024

   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      Dim path As String = Request.Params("File")
      path = "\\speedy\wanfiles\" + path.Substring(3)
      Dim file As System.IO.FileInfo = New System.IO.FileInfo(path)
      Dim Buffer(BufferSize) As Byte
      Dim SizeWritten, fileindex As Integer
      Response.Clear()

      Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name)
      Response.AddHeader("Content-Length", file.Length.ToString)

      Response.ContentType = "application/octet-type"
      Response.Flush()

      fileindex = 0
      Do
         // not sure about this GM.AlasData code below
         SizeWritten = GM.AlasData.ReadFileBlock(file.FullName, Buffer, fileindex, BufferSize) 
         Response.OutputStream.Write(Buffer, 0, SizeWritten)
         Response.Flush()
         If SizeWritten < BufferSize Then
            Exit Do
         Else
            fileindex = fileindex + SizeWritten
         End If
      Loop

      Response.End()


   End Sub

End Class

このコードはMVC3を使用してダウンロードを実行することがわかりましたが、ローカルファイルと見なされるため、ファイルにアクセスできません。

public FileResult Download(string FilePath)
{
    if (FilePath != null)
    {
        string path = FilePath;
        string contentType;
        // files are stored on network server named speedy
        path = string.Concat(@"\\speedy\files\", HttpUtility.UrlDecode(path));
        System.IO.FileInfo file = new System.IO.FileInfo(path);
        Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(file.Extension.ToLower());
        if (rk != null && rk.GetValue("Content Type") != null)
        {
            contentType = rk.GetValue("Content Type").ToString();
        }
        else
        {
            contentType = "application/octet-type";
        }

        //Parameters to file are
        //1. The File Path on the File Server
        //2. The content type MIME type
        //3. The parameter for the file save by the browser
        return File(file.FullName, contentType, file.Name);

    }
    else
    {
        return null;
    }
}

この機能を実行できる場所でこれを修正するには、何をする必要がありますか?

4

2 に答える 2

1

フォーム認証を使用しているため、認証モード=Windowsの行を削除できます。偽装を使用して、アクセスしようとしているネットワークの場所への読み取りアクセス権がアカウントにあることを確認してください。それはうまくいくはずです。

于 2012-09-25T02:35:00.190 に答える
0

http://www.codeproject.com/Articles/4051/Windows-Impersonation-using-C

于 2012-09-28T20:53:21.403 に答える