2017-09-06 1 views
1

Webアプリケーションの認証を実行して、異なる権限を持つユーザーが指定された情報にアクセスできるようにする必要があります。ユーザーの認証を有効にするためにIntroduction to Identity on ASP.NET Core(MSDN)をフォローしても問題はありません。その後、私はCreate an ASP.NET Core app with user data protected by authorization(MSDN)の承認を受けましたが、それ以上は進めませんでした。私はこのプログラムを実行するとASP.Net Webアプリケーションの認証と承認の問題

...

public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
{ 
    if (env.IsDevelopment()) 
    { 
     app.UseDeveloperExceptionPage(); 
     app.UseDatabaseErrorPage(); 
    } 
    else 
    { 
     app.UseExceptionHandler("/Home/Error"); 
    } 

    app.UseStaticFiles(); 

    app.UseIdentity(); 

    app.UseMvcWithDefaultRoute(); 

    // Set password with the Secret Manager tool. 
    // dotnet user-secrets set SeedUserPW <pw> 
    var testUserPw = Configuration["SeedUserPW"]; 

    if (String.IsNullOrEmpty(testUserPw)) 
    { 
     throw new System.Exception("Use secrets manager to set SeedUserPW \n" + 
            "dotnet user-secrets set SeedUserPW <pw>"); 
    } 

    try 
    { 
     SeedData.Initialize(app.ApplicationServices, testUserPw).Wait(); 
    } 
    catch 
    { 
     throw new System.Exception(@"You need to update the DB 
      \nPM > Update-Database \n or \n 
       > dotnet ef database update 
       \nIf that doesn't work, comment out SeedData and 
       register a new user"); 
    } 

は...私はこのエラーを取得する:

System.Exception: 'You need to update the DB PM > Update-Database or > dotnet ef database update If that doesn't work, comment out SeedData and register a new user'

私は、データベースを更新し、更新の成功を受けたが、エラーがまだ残っています。私もユーザーとパスワードを変更しましたが、何も起こりませんでした。

Webアプリケーションで認証と承認を有効にするにはどうすればよいですか?

+0

あなたのコードを表示し、自分で実装した例外について私たちに教えてください。ほとんど役に立ちません。代わりに、 'try'' catch'ブロックコードの全てを* SeedData.Initialize(app.ApplicationServices、testUserPw).Wait();'に置き換えて、あなたのプログラムを実行し、どのエラーが発生するか教えてください投げられた? –

+0

こんにちは@QualityCatalyst、私は問題を追いかけて、例外が発生しました。 'ArgumentNullException:値はnullにはなりません。 'プログラムがseedDataのcalssメソッド" EnsureRole "を読み込むと例外がスローされます。 'var user = await userManager.FindByIdAsync(uid);'ユーザがnullであるためです。 –

答えて

0

上記のコードはCore 1.X用です.2.0では、すべての初期化コードをProgram.csに移動することをお勧めします。空のパスワードテストとSeedData.InitializeをStartup.csから削除してください。 SeedData.csファイルをまったく変更する必要はありません。

namespace YourApp 
{ 
    public class Program 
    { 
     public static void Main(string[] args) 
     { 

      var host = BuildWebHost(args); 

      using (var scope = host.Services.CreateScope()) 
      { 
       var env = scope.ServiceProvider.GetRequiredService<IHostingEnvironment>(); 

       if(env.IsDevelopment()) 
       { 
        var services = scope.ServiceProvider; 
        // You can get the Configuration directly withou DI 
        // var config = new ConfigurationBuilder().AddUserSecrets<Startup>().Build(); 
        var config = services.GetRequiredService<IConfiguration>(); 

        string testUserPw = config["SeedUserPW"]; 
        if (String.IsNullOrEmpty(testUserPw)) 
        { 
         throw new Exception("Use secrets manager to set SeedUserPW \n" + 
             "dotnet user-secrets set SeedUserPW <pw>"); 

        } 

        try 
        { 
         SeedData.Initialize(services, testUserPw).Wait(); 
        } 
        catch 
        { 
         throw new Exception("You need to update the DB " 
         + "\nPM > Update-Database " + "\n or \n" + 
          "> dotnet ef database update" 
          + "\nIf that doesn't work, comment out SeedData and " 
          + "register a new user"); 
        } 
       } 
      } 
      host.Run(); 
     } 

     public static IWebHost BuildWebHost(string[] args) => 
     WebHost.CreateDefaultBuilder(args) 
       .UseStartup<Startup>() 
       .Build(); 
    } 
} 
関連する問題