0

MigrationsWeb アプリからクラス ライブラリ プロジェクトに移植しました。を呼び出せないことを除いて、すべて正常に動作しstatic class Rolesます。

が配置されている名前空間using System.Web.Security;を含めましたRoles

Configuration.cs ファイルの内容は次のとおりです。

namespace _DataContext.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using WebMatrix.WebData;
    using System.Web.Security;

internal sealed class Configuration : DbMigrationsConfiguration<_DataContext.DataContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(_DataContext.DataContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //

        SeedMembership();
    }

    private void SeedMembership()
    {
        WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

        // doesn't work either:
        //var roles = (SimpleRoleProvider)Roles.Provider;
        //var membership = (SimpleMembershipProvider)Membership.Provider;

        if (Roles.RoleExists("Administrator"))
          Roles.CreateRole("Administrator");
    }
  }
}

エラーメッセージは次のとおりです。

The name 'Roles' does not exist in the current context

ここで何が欠けていますか?

[編集]

私はさらに調査を行ってきましたが、メソッドSimpleRoleProviderにアクセスするにはオブジェクトを作成する必要があるようです。RoleExists

しかし、なぜ私はこのようにしなければならないのですか?なぜ使用できないのですか:

if (Roles.RoleExists("Administrator"))              
   Roles.CreateRole("Administrator");

Rolesから来static classますか?

4

3 に答える 3

3

ファイルのセクションにroleManager要素を追加しましたか? Roles の MSDN ページから:system.webWeb.config

ASP.NET アプリケーションのロール管理を有効にするには、次の例に示すように、アプリケーションの Web.config ファイルの system.web セクションの roleManager 要素を使用します。

セクションは次のようになります。

  <roleManager defaultProvider="SqlProvider" 
    enabled="true"
    cacheRolesInCookie="true"
    cookieName=".ASPROLES"
    cookieTimeout="30"
    cookiePath="/"
    cookieRequireSSL="false"
    cookieSlidingExpiration="true"
    cookieProtection="All" >
    <providers>
      <add
        name="SqlProvider"
        type="System.Web.Security.SqlRoleProvider"
        connectionStringName="SqlServices"
        applicationName="SampleApplication" />
      </providers>
    </roleManager>
于 2013-05-09T13:06:49.597 に答える
1

ロールに直接アクセスできるはずですが、 SimpleMembershipプロバイダーを使用する場合はお勧めしません。そうは言っても、プロジェクトでSystem.Webアセンブリを参照していますか?

ロール プロバイダーを取得するための推奨される方法は、次のようにすることです。

  var roles = (WebMatrix.WebData.SimpleRoleProvider)Roles.Provider;

  if (!roles.RoleExists("Admin"))
  {
      roles.CreateRole("Admin");
  }

RolesSimpleRoleProviderの定義を比較すると、かなりの違いがあることがわかります。SimpleRoleProviderはRolesの完全なインターフェースを実装していないように見えますが、これはカスタム プロバイダーを実装する場合には必要ありません。ロールから直接呼び出すと、一部のメソッドで「実装されていない」例外が発生する場合があります。 SimpleRoleProviderは、 SimpleMembershipを使用するときに役立つ追加のメソッド/プロパティも提供します。

于 2013-05-09T15:10:17.970 に答える