2017-09-06 126 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(); 
    } 
}