Medium Trustで作業しているときに、anonymousIdentificationセクションのweb.configからcookieNameを読み取る簡単な方法はありますか?
私がやろうとしているのは、次のいずれかの理由で、何千もの匿名ユーザーの作成を防ぐことです。
- 人々はクッキーをオフにしている、または
- 訪問者はCookie機能のないスパイダー/ボットです。
これを実現しようとしているのは、Application_BeginRequestにFormsAuthenticationまたはAnonymousIdentificationCookieが存在するかどうかを確認することです。Cookieがない場合は、データベースに何も保存されないようにするフラグを設定します。
しかし、それを行うには、Cookieの名前を知っている必要があります。そのために、私はこれを試みました:
AuthCookieName = FormsAuthentication.FormsCookieName;
var anonSection = (AnonymousIdentificationSection)WebConfigurationManager.GetSection("system.web/anonymousIdentification");
if (anonSection != null)
AnonCookieName = anonSection.CookieName;
認証Cookie名は問題なく取得されますが、WebConfigurationManagerはセキュリティ例外をスローします。System.Security.SecurityException:タイプ'System.Configuration.ConfigurationPermission、System.Configuration、Version = 4.0.0.0、Culture=neutralのアクセス許可の要求、PublicKeyToken =b03f5f7f11d50a3a'が失敗しました。
高信頼または完全信頼を与えると例外がなくなるため、これが信頼の問題であることを私は知っています。ただし、これがMedium Trustで機能することが重要であり、machine.configを変更することはできません。
requirePermission="false"
私のアプリケーションのweb.configレベルでanonymousIdentificationセクションを設定する方法はありますか?
web.configをXMLドキュメントにロードし、手動で解析する必要がありますか?
他のアイデア?
これより良いものはありますか?で1回だけ実行していApplication_Start()
ます。
XmlDocument config = new XmlDocument();
config.Load(Server.MapPath("~/Web.config"));
XmlNode anonSection = config.SelectSingleNode("configuration/system.web/anonymousIdentification");
if (anonSection != null)
{
XmlAttribute nameAttr = anonSection.Attributes["cookieName"];
if (nameAttr != null)
AnonCookieName = nameAttr.Value;
}
if (string.IsNullOrWhiteSpace(AnonCookieName))
AnonCookieName = ".ASPXANONYMOUS";