2 に答える
これらの手順により、必要な動作を実装できるようになります。
要約する:
- ルーティングを使用していますが、MVC は使用していません。私の例では、http://host/Mysite/userid/12345のような URL をhttp://host/Mysite/Pages/users.aspx?userid=12345の実際のページにマップします。
- これらのアドレスへのアクセスを制御して、ユーザーにログオンを要求します。私の例には、標準のログイン コントロールを備えたページhttp://host/Mysite/login.aspxがあり、サイトはフォーム認証を使用するように構成されています。
ステップ1
Pages フォルダー内のこの web.config を使用して、Pages フォルダーの内容を「非表示」にしました。
<?xml version="1.0"?>
<configuration>
<system.web>
<httpHandlers>
<add path="*" verb="*"
type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<pages validateRequest="false">
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
</handlers>
</system.webServer>
</configuration>
これにより、だれかがhttp://host/Mysite/Pages/users.aspx?userid=12345のような URL を使用すると、標準の 404 応答を受け取ることが保証されます。
ステップ2
私の最上位の web.config ファイルには、(すべての標準的なものと同様に) この場所の要素が含まれています。
<location path="userid">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
これにより、 http://host/Mysite/userid/12345の形式の URL への匿名アクセスが防止されます。これは、ユーザーが自動的に login.aspx にリダイレクトされ、有効な資格情報を提供すると、正しい場所にリダイレクトされることを意味します。
ステップ 3
参考までに、私のglobal.asaxは次のとおりです。
<script RunAt="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.RouteExistingFiles = true;
routes.Add("UseridRoute", new Route
(
"userid/{userid}",
new CustomRouteHandler("~/Pages/users.aspx")
));
}
</script>
そして、ここに私のルートハンドラがあります:
using System.Web.Compilation;
using System.Web.UI;
using System.Web;
using System.Web.Routing;
using System.Security;
using System.Web.Security;
public interface IRoutablePage
{
RequestContext RequestContext { set; }
}
public class CustomRouteHandler : IRouteHandler
{
public CustomRouteHandler(string virtualPath)
{
this.VirtualPath = virtualPath;
}
public string VirtualPath { get; private set; }
public IHttpHandler GetHttpHandler(RequestContext
requestContext)
{
var page = BuildManager.CreateInstanceFromVirtualPath
(VirtualPath, typeof(Page)) as IHttpHandler;
if (page != null)
{
var routablePage = page as IRoutablePage;
if (routablePage != null) routablePage.RequestContext = requestContext;
}
return page;
}
}
The first result I got from a Google search is Frederiks excellent post on forms authentication in ASP.NET MVC. Note that the post was relevant for an early version of ASP.NET MVC, you will have to write and test the code.
HTH, Indy