デフォルトでは、.NET認証はpdfsなどの静的ファイルでは機能しません。
ユーザーが認証されている場合は、ファイルを処理するためにHTTPハンドラを実装する必要があります。
あなたの現在の認証が設定され、正しく動作しているように聞こえるので、設定の基本については説明しません。
以下
ここKoryベッカー役立つ記事から取られたシナリオに適用され、関連するコードです:
http://www.primaryobjects.com/2009/11/11/securing-pdf-files-in-asp-net-with-custom-http-handlers
あなたは明らかに、ご使用の環境に合わせてパス、名前空間とロジックを変更する必要があります(たとえば、 IISバージョン)および/または特定のファイルタイプ要件。
ステップ1 - (適切なパス/名前空間の変更で)あなたのweb.configファイルに次のセクションを追加します
<httpHandlers>
...
<add path="*/User/Documents/*.pdf" verb="*" validate="true" type="CustomFileHandlerDemo.Handlers.FileProtectionHandler" />
</httpHandlers>
<system.webServer>
...
<handlers>
<add name="PDF" path="*.pdf" verb="*" type="CustomFileHandlerDemo.Handlers.FileProtectionHandler" resourceType="Unspecified" />
...
</handlers>
</system.webServer>
から
public class FileProtectionHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
switch (context.Request.HttpMethod)
{
case "GET":
{
// Is the user logged-in?
if (!context.User.Identity.IsAuthenticated)
{
FormsAuthentication.RedirectToLoginPage();
return;
}
string requestedFile = context.Server.MapPath(context.Request.FilePath);
// Verify the user has access to the User role.
if (context.User.IsInRole("Security Alerts - Admin"))
{
SendContentTypeAndFile(context, requestedFile);
}
else
{
// Deny access, redirect to error page or back to login page.
context.Response.Redirect("~/User/AccessDenied.aspx");
}
break;
}
}
}
public bool IsReusable { get; private set; }
private HttpContext SendContentTypeAndFile(HttpContext context, String strFile)
{
context.Response.ContentType = GetContentType(strFile);
context.Response.TransmitFile(strFile);
context.Response.End();
return context;
}
private string GetContentType(string filename)
{
// used to set the encoding for the reponse stream
string res = null;
FileInfo fileinfo = new FileInfo(filename);
if (fileinfo.Exists)
{
switch (fileinfo.Extension.Remove(0, 1).ToLower())
{
case "pdf":
{
res = "application/pdf";
break;
}
}
return res;
}
return null;
}
}
ステップ2 IHTTPハンドラを実装FileProtectionHandlerクラスを作成します。
ありがとう@Scotty - 私はこれを試して、それがどのようになっているかを知らせます。 – KlydeMonroe
こんにちは@Scotty - 私はあなたが上の例に従っているように実装に取り組んできました。それは動作していないようです。私は 'Handlers'と呼ばれる新しいディレクトリを作成し、クラス 'FileProtectionHandler'を作成し、私のアプリケーションのLogin.aspxへのリダイレクトを変更しました。私はその後、アプリケーションのメインweb.configファイルにビットを追加しました - 私のアプリケーション '*/documents/*。pdf'とtype = 'Alerts.Handlers.FileProtectionHandler'に合うようにパスを変更して、IIS7を使用していますこれへのハンドラーマッピング。私は今、HTTP 500.23エラー、任意の提案を取得していますか? – KlydeMonroe
Web設定ファイルのハンドラタグの前に、 を追加することで、これを動作させることができました。あなたの助けをありがとう –
KlydeMonroe