mar*_*ode 5 c# authentication entity-framework asp.net-core
我正在尝试向我的应用程序添加身份验证,我有实体框架运行但现在我想验证用户,但我遇到很多问题在配置构造函数中配置它.
例如,在许多教程中,他们提供的代码不再像我那样工作
// Configure ASP.NET Identity to use our Identity-based application context
services.AddAuthentication()
.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)
它告诉我,我需要明确指定类型参数,但这是教程中的内容吗?
https://shellmonger.com/2015/05/29/asp-net-mvc5-identity-part-1-the-database/
我很难理解这样做的正确方法,我想要做的就是在用户登录时对用户进行身份验证.
这是我的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"
},
Run Code Online (Sandbox Code Playgroud)
和我的配置:
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);
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,我的想法已经用完了,我已经尝试了几个具有相同结果的其他网站(这种方式做了改变吗?).
您可以在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();
Run Code Online (Sandbox Code Playgroud)
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);
}
};
});
}
Run Code Online (Sandbox Code Playgroud)
更多信息: ASP.NET身份验证文档
| 归档时间: |
|
| 查看次数: |
7277 次 |
| 最近记录: |