我正在编写一个封闭的ASP .NET MVC 5.1应用程序。只有经授权的人才能进去。我想从应用程序中删除注册操作。我可以通过在与web应用程序关联的数据库中添加新行来手动添加用户吗?
如何在2013中这样做?
发布于 2014-08-02 09:21:57
将用户直接添加到数据库表通常并不容易,因为有许多相互关联的问题,如权限、角色和密码哈希。
但是,可以在代码中“为”数据库“种子”。下面是一个使用Seed
标识的ASP.NET函数的示例。
protected override void Seed(ApplicationDbContext context)
{
//First, access the UserManager
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
//Secondly, Create the user account
var user = new ApplicationUser
{
UserName = "ExampleUser",
UserProfileInfo = new UserProfileInfo
{
FirstName = "Example",
LastName = "User",
EmailID = "exampleuser@testdomain.com"
}
};
//Last, add the user to the database
manager.Create(user, "password123");
}
此函数将在下次Update-Database
在NuGet Package控制台中运行时运行。
https://stackoverflow.com/questions/25097423
复制