我正在编写一个封闭的ASP .NET MVC 5.1应用程序。只有授权人员可以进入。我要从应用程序中删除注册操作。是否可以通过在与Web应用程序关联的数据库中添加新行来手动添加用户?
如何在Visual Studio 2013中执行此操作?
通常,将用户直接添加到数据库表并不容易,因为存在许多相互关联的问题,例如权限,角色和密码哈希。
但是,可以用代码“播种”数据库。以下是Seed
使用ASP.NET Identity的函数的示例。
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 = "[email protected]"
}
};
//Last, add the user to the database
manager.Create(user, "password123");
}
此功能将在下一次Update-Database
在NuGet软件包管理器控制台中运行时运行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句