0

ASP.NET MVC4 アプリケーションにシンプル メンバーシップを実装しました。しかし今、ユーザーの作成を実装し、ログインを確認する必要があります。

そこで、最近のプロジェクトのコードの一部をこれにコピーし始めました。例によって、これはクラッシュする部分です

            WebSecurity.InitializeDatabaseConnection("SimpleMembershipConnection",
  "UserProfile", "UserId", "UserName", autoCreateTables: true);
            bool val = WebSecurity.Login("1010", "pass"); // here throws the exception

エラーは言う:

オブジェクト参照がオブジェクト インスタンスに設定されていません。パラメータ名: httpContext.

また、いくつかのライブラリを WebMatrix.WebData、WebMatrix.Data、および System.Web としてインポートするだけです。

これは私のapp.config全体です

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
     ...
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
<roleManager enabled="true" defaultProvider="simple">
  <providers>
    <clear />
    <add name="simple" type="WebMatrix.WebData.SimpleRoleProvider,              WebMatrix.WebData" />
  </providers>
</roleManager>
<membership defaultProvider="simple">
  <providers>
    <clear />
    <add name="simple" type="WebMatrix.WebData.SimpleMembershipProvider,              WebMatrix.WebData" />
  </providers>
</membership>

HttpContext で null を取得しないという例外を修正するにはどうすればよいですか?

4

1 に答える 1

1

いいえ。WinFormsでは、メンバーシッププロバイダーのライフサイクルに従う必要はありません。WinFormsでWeb要求用のdllを使用する場合、HttpContextは設定されません。HttpContextは、現在のWebリクエストを参照します。これは、winformsには明らかに欠けています。

認証ビジネスロジックを分離して、別のdllに配置することができます。このライブラリは、Windowsアプリケーションとメンバーシッププロバイダーの両方で参照できます。

Webを介してユーザーを認証する必要がある場合、Windowsアプリケーションでこれを行う方法は、Webサービスにクエリを実行することです。次に、サーバー側でSimpleMembershipProviderを使用できます。

于 2013-01-15T06:08:00.220 に答える