【发布时间】:2016-04-13 03:11:01
【问题描述】:
我正在尝试向我的应用程序添加身份验证,我正在运行实体框架,但现在我想对用户进行身份验证,但在配置构造函数中配置它时遇到了很多问题。
例如,在许多教程中,他们提供的代码不再像我那样工作
// Configure ASP.NET Identity to use our Identity-based application context
services.AddAuthentication()
.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
它告诉我需要明确指定类型参数,但这就是教程中的内容?
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"
},
和我的配置:
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 版本中找到。
-
一直没找到,有没有可以帮我的链接?,也许我搜索错了。谢谢
-
请不要混用包版本,这是自找麻烦。您的 project.json 是 rc1-final、beta4、beta5、beta7 和 beta8 的混乱组合。
-
你可以在官方文档中找到一个例子:docs.asp.net/en/latest/security/authentication/…
标签: c# entity-framework authentication asp.net-core