2017-09-06 2 views
1

Ich muss Authentifizierung für meine Webanwendung ausführen, damit verschiedene autorisierte Benutzer Zugriff auf angegebene Information haben. Ich folgte Introduction to Identity on ASP.NET Core (MSDN) als die Authentifizierung für meine Benutzer aktivieren und es sieht OK aus. Ich folgte dann (MSDN) zur Autorisierung, aber ich konnte nicht weiter gehen.Authentifizierung und Autorisierungsprobleme in ASP.Net-Webanwendung

Als ich dieses Programm ausführen ...

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"); 
    } 

... Ich bekomme diese Fehlermeldung:

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'

ich die Datenbank aktualisiert und erhielt erfolgreiches Update, aber der Fehler blieb noch. Ich habe auch den Benutzer und das Passwort geändert, aber nichts ist passiert.

Wie kann ich die Authentifizierung und Autorisierung in meiner Webanwendung aktivieren?

+0

Zeigen Sie uns Ihren Code und erzählen uns von einer Ausnahme, die geworfen wird, die Sie selbst implementiert haben, ist wenig hilfreich. Würden Sie bitte stattdessen * all * Ihres 'try'' catch'-Block-Codes mit folgendem ersetzen: 'SeedData.Initialize (app.ApplicationServices, testUserPw) .Wait();', führen Sie Ihr Programm aus und sagen Sie uns, welcher Fehler auftritt geworfen? –

+0

Hallo @QualityCatalyst, ich habe das Problem gejagt und ich bekam eine Ausnahme: 'ArgumentNullException: Wert kann nicht null sein.Ausnahme wird ausgelöst, wenn das Programm seedData calss, Methode" EnsureRole "liest. 'var user = awarteManager.FindByIdAsync (uid);' weil der Benutzer null ist. –

Antwort

0

Der obige Code ist für Core 1.X, ab 2.0 ist die beste Vorgehensweise, den gesamten Initialisierungscode auf Program.cs zu verschieben, also entfernen Sie bitte das leere Passwort und SeedData.Initialize aus Startup.cs Sie müssen die Datei SeedData.cs nicht ändern.

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(); 
    } 
}