2017-09-06 82 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:值不能爲空.'當程序讀取seedData calss,方法「EnsureRole」時拋出異常。 'var user = await userManager.FindByIdAsync(uid);'因爲用戶爲空。 –

回答

0

上面的代碼是針對Core 1.X的,從2.0開始,最好的做法是將所有的初始化代碼移動到Program.cs中,所以請從Startup.cs中刪除空密碼測試和SeedData.Initialize,根本不需要修改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(); 
    } 
}