0中配置身份验证

0中配置身份验证

本文介绍了如何在ASP.NET Core 1.0中配置身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加身份验证到我的应用程序,我有实体框架运行,但现在我想验证用户,但我在配置构造函数中配置很多问题。



例如,在许多教程中,他们提供的代码不再像以下那样工作:

 code> //配置ASP.NET身份以使用我们基于身份的应用程序上下文
services.AddAuthentication()
.AddIdentity()
.AddEntityFrameworkStores()
。 AddDefaultTokenProviders();

它告诉我,我需要明确指定类型参数,但这是在教程中?





我很难理解这样做的正确方法,所有我想做的是在登录时验证用户。



这是我的project.json

 依赖关系:{
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-最终,
Microsoft.AspNet.Identity:3.0.0-rc1-final,
Microsoft.AspNet.Identity.EntityFr工作: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
},
/ pre>

和我的配置:

  public class Startup 
{
public IConfigurationRoot Configuration {get;组;

public Startup()
{

var builder = new ConfigurationBuilder()
.AddJsonFile(config.json)
.AddJsonFile($config.json,可选:true);
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
//这个方法被运行时调用。使用此方法向容器添加服务。
//有关如何配置应用程序的更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext< OrganizationsAppContext>(options =>
options.UseSqlServer(Configuration [Data:DefaultConnection:ConnectionString])) ;

//指定应用程序数据库上下文的配置
services.Configure< Option>(options =>
{
options.DefaultUserName = Configuration.Get DefaultUser:Username);
options.DefaultUserPassword = Configuration.Get(DefaultUSer:Password);
});

//配置ASP.NET身份以使用我们基于身份的应用程序上下文
//services.AddAuthentication()
// .AddIdentity()
// .AddEntityFrameworkStores()
// .AddDefaultTokenProviders();不工作!

services.AddMvc();
}

//这个方法被运行时调用。使用此方法配置HTTP请求管道。
public void Configure(IApplicationBuilder app)
{
app.UseIISPlatformHandler();
app.UseMvc();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseIdentity();
}

//应用程序的入口点。
public static void Main(string [] args)=> WebApplication.Run<启动>(参数);
}

任何帮助将不胜感激,我没有想法,我试过几个其他网站的结果相同(这样做是否改变了?)。

解决方案

您可以通过两种方式配置身份在RC1中:



1-添加身份时



示例:

  services.AddIdentity< User,Role>(config => {
//在此配置
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);
}
};
});
}

更多信息:


I am trying to add authentication to my application, i have entity framework running but now i would like to authenticate the user but i am running into lots of issues configuring it in the configure constructor.

For instance in many tutorials they provide code that no longer works like if I do

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

It tells me that i need to specify the type arguments explicitly, but this is what is in the tutorial?

https://shellmonger.com/2015/05/29/asp-net-mvc5-identity-part-1-the-database/

I am having a hard time understanding what is the right way of doing this, all i want to do is authenticate an user when he/she logs in.

Here is my project.json

  "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"
  },

and my configuration :

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

Any help would be appreciated, i am running out of ideas, i have tried several other sites with the same result (did this way of doing it change?).

解决方案

You can configure identity in two ways in RC1:

1- When you are adding Identity

Example:

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- Use IdentityOptions:

Example:

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

More info:ASP.NET Authentication Doc

这篇关于如何在ASP.NET Core 1.0中配置身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 00:44