0

In order to get a better understanding of how to wire in auth functionality in .net MVC4 application, I thought I'd take apart the website that the 'Internet Application' template produces. Ultimately I'm looking into how I would implement something that is claims aware (such as this: https://github.com/brockallen/BrockAllen.MembershipReboot)

As part of this I've found myself looking at the method WebMatrix.WebData.WebSecurity.PreAppStartInit() which contains the following code:

 const string BuiltInMembershipProviderName = "AspNetSqlMembershipProvider";
    var builtInMembership = Membership.Providers[BuiltInMembershipProviderName];
    if (builtInMembership != null)
    {
        var simpleMembership = CreateDefaultSimpleMembershipProvider(BuiltInMembershipProviderName, currentDefault: builtInMembership);
        Membership.Providers.Remove(BuiltInMembershipProviderName);
        Membership.Providers.Add(simpleMembership);
    }

If I try to run something similar in a console app it throws a System.NotSupportedException at the point Membership.Providers.Remove(BuiltInMembershipProviderName); is called. This is to be expected if the collection has had its SetReadOnly method called, but it hasn't. I'm not sure why this is happening in the console app code, but not the Web Application. Anyone have any ideas?

4

1 に答える 1

0

これは私もしばらく混乱しました。コツは、この初期化のタイミングです。非常に早い段階で実行する必要があります。WebMatrix.WebData 名前空間を掘り下げている間、それがどのように機能するかを確認するために他にもいくつかの部分を確認する必要があります。

PreApplicationStartMethodアセンブリ属性が適用されています。

[assembly: PreApplicationStartMethod(typeof(PreApplicationStartCode), "Start")]

この属性で指定されたメソッドは、アプリケーションの起動シーケンスの一部として呼び出されます。PreApplicationStartCode.Start() メソッドは WebSecurity.PreAppStartInit() を呼び出し、その他の構成タスクを実行します。

同じことを行うことで、この機能を自分で複製できます。アセンブリに PreApplicationStartMethod 属性を追加し、独自の「スタートアップ」メソッドを呼び出すようにします。この段階で、Membership providers コレクションをいじることができます。

于 2013-08-04T23:48:41.053 に答える