ASP.Net Web应用程序中的身份验证和授权问题

时间:2017-09-06 04:59:12

标签: c# asp.net database authentication authorization

我需要为我的Web应用程序运行身份验证,以便不同的授权用户可以访问指定的信息。我跟着Introduction to Identity on ASP.NET Core(MSDN)作为激活我的用户的身份验证,看起来没问题。然后我按照Create an ASP.NET Core app with user data protected by authorization(MSDN)进行授权,但我无法继续进行。

当我运行此程序时......

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:&#39;您需要更新DB PM&gt;更新数据库或&gt;   dotnet ef数据库更新如果这不起作用,请注释掉SeedData   并注册一个新用户&#39;

我更新了数据库并收到了成功的更新,但错误仍然存​​在。我也改变了用户和密码但没有发生任何事情。

如何在我的Web应用程序上激活身份验证和授权?

1 个答案:

答案 0 :(得分:0)

上面的代码适用于Core 1.X,从2.0开始,最佳做法是将所有初始化代码移到Program.cs,所以请从Startup.cs中删除空密码测试和SeedData.Initialize,其中&# 39;根本不需要修改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();
    }
}