自分が毎回新規で ASP.NET MVC アプリケーションの開発を始めるときに忘れるので、今までブログに書いた分をまとめてみました。
ASP.NET MVC 5 と限定してあるのは、5 より前のバージョンではバグ持ちの設定を紹介しているからです。
Razor のみを使う
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
モバイルビューを無効にする
DisplayModeProvider.Instance.Modes.Clear();
DisplayModeProvider.Instance.Modes.Add(new DefaultDisplayMode());
生成される URL を小文字にする
public static void RegisterRoutes(RouteCollection routes) { routes.LowercaseUrls = true; }
生成される URL の最後にスラッシュを付ける
public static void RegisterRoutes(RouteCollection routes) { routes.AppendTrailingSlash = true; }
認証クッキーの名前を変更する
<authentication mode="Forms"> <forms name="auth" /> </authentication>
CSRF 用クッキーの名前を変更する
AntiForgeryConfig.CookieName = "token";
セッションクッキーの名前を変更する
<system.web> <sessionState cookieName="session" /> </system.web>
認証クッキーで SSL を必須にする
<authentication mode="Forms"> <forms requireSSL="true" /> </authentication>
CSRF 用クッキーで SSL を必須にする
AntiForgeryConfig.RequireSsl = true;
認証クッキー以外に httpOnly, secure 属性を追加する
<system.web> <httpCookies httpOnlyCookies="true" requireSSL="true" /> </system.web>
AntiForgery 使用時に X-Frame-Options ヘッダを出力しない
AntiForgeryConfig.SuppressXFrameOptionsHeader = true;
レスポンスヘッダに ASP.NET のバージョンを出力しない
<system.web> <httpRuntime targetFramework="4.5" enableVersionHeader="false" /> </system.web>
レスポンスヘッダに ASP.NET MVC のバージョンを出力しない
MvcHandler.DisableMvcResponseHeader = true;
レスポンスヘッダに X-Powered-By を出力しない
<system.webServer> <httpProtocol> <customHeaders> <remove name="X-Powered-By" /> </customHeaders> </httpProtocol> </system.webServer>
レスポンスヘッダに Server を出力しない
protected void Application_PreSendRequestHeaders() { Response.Headers.Remove("Server"); }
IP アドレスでアクセス制限を行う
<system.webServer> <security> <ipSecurity allowUnlisted="false" denyAction="NotFound"> <add allowed="true" ipAddress="127.0.0.1"/> <add allowed="true" ipAddress="::0"/> </ipSecurity> </security> </system.webServer>
最後の方は設定という感じではなかったですが、まあ気にしない方向でお願いします。