如何在ASP.NET Core 1.0中配置身份验证

5

我正在尝试为我的应用程序添加身份验证功能,我已经在使用实体框架,但现在我想对用户进行身份验证,但是在配置构造函数时遇到了很多问题。

例如,在许多教程中,他们提供的代码不再适用,例如如果我执行:

    // Configure ASP.NET Identity to use our Identity-based application context
    services.AddAuthentication()
        .AddIdentity()
        .AddEntityFrameworkStores()
        .AddDefaultTokenProviders();

它告诉我需要明确指定类型参数,但这就是教程中的内容?我很难理解正确的做法是什么,我只想在用户登录时对其进行身份验证。以下是我的project.json文件。https://shellmonger.com/2015/05/29/asp-net-mvc5-identity-part-1-the-database/
  "dependencies": {
    "EntityFramework.Commands": "7.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "EntityFramework.Core": "7.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-rc1-final",
    "Microsoft.AspNet.Identity": "3.0.0-rc1-final",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
    "Microsoft.AspNet.Authentication": "1.0.0-rc1-final",
    "Microsoft.AspNet.Authorization": "1.0.0-rc1-final",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.Framework.CodeGenerators.Mvc": "1.0.0-beta5",
    "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta4",
    "Microsoft.Framework.Logging": "1.0.0-beta7",
    "Microsoft.Framework.Logging.Console": "1.0.0-beta8",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final"
  },

我的配置如下:

public class Startup
{
    public IConfigurationRoot Configuration { get; set; }

    public Startup()
    {

        var builder = new ConfigurationBuilder()
        .AddJsonFile("config.json")
        .AddJsonFile($"config.json", optional: true);
        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<OrganizationsAppContext>(options =>
            options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

        // Specify the configuration of our Application database context
        services.Configure<Option>(options =>
        {
            options.DefaultUserName = Configuration.Get("DefaultUser:Username");
            options.DefaultUserPassword = Configuration.Get("DefaultUSer:Password");
        });

        // Configure ASP.NET Identity to use our Identity-based application context
        //services.AddAuthentication()
        //    .AddIdentity()
        //    .AddEntityFrameworkStores()
        //    .AddDefaultTokenProviders();   DOES NOT WORK!

        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
        app.UseIISPlatformHandler();
        app.UseMvc();
        app.UseDefaultFiles();
        app.UseStaticFiles();
        app.UseIdentity();
    }

    // Entry point for the application.
    public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}

任何帮助都将不胜感激,我已经想尽了办法,我已经在几个其他网站上尝试过,结果相同(这种方法改变了吗?)。


请注意,ASP.NET Core 1.0仍未完成。该教程似乎是去年五月份发布的,已经过去了将近一年时间。自那时以来,发生了很多变化。尽量寻找更近期的教程来帮助您,最好是从现在发布的RC版本中获取。 - Sami Kuhmonen
我找不到,你有一个链接可以帮助我吗?也许是我搜索的方式不对。谢谢。 - mary_code
请不要混合使用不同的软件包版本,这会引起问题。你的project.json文件中混杂了rc1-final、beta4、beta5、beta7和beta8等多个版本,非常混乱。 - Tseng
1
你可以在官方文档中找到一个例子:https://docs.asp.net/en/latest/security/authentication/introduction-to-aspnet-identity.html - Fabien ESCOFFIER
1个回答

9
您可以在 RC1 中以两种方式配置身份验证:
1- 添加身份验证时
例如:
services.AddIdentity<User, Role>(config => {
    // Config here
    config.User.RequireUniqueEmail = true;
    config.Password = new PasswordOptions
    {
        RequireDigit = true,
        RequireNonLetterOrDigit = false,
        RequireUppercase = false,
        RequireLowercase = true,
        RequiredLength = 8,
    }; 
}).AddEntityFrameworkStores<ApplicationContext, int>() 
  .AddDefaultTokenProviders();

2- 使用 IdentityOptions:

示例:

services.Configure<IdentityOptions>(options =>
    {
        options.Password = new PasswordOptions
        {
            RequireDigit = true,
            RequireNonLetterOrDigit = false,
            RequireUppercase = false,
            RequireLowercase = true,
            RequiredLength = 8,
        };
        options.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents
        {
            OnRedirectToLogin = ctx =>
            {
                ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return Task.FromResult<object>(null);
            }
        };
    });
}

更多信息: ASP.NET身份验证文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接