我正在使用内部网,到目前为止,我已经从Windows身份验证切换到表单身份验证,我可以连接/注册等。
但这是我期望做的事情:我希望链接员工列表(其中包含许多参数),而不是通过常规表单路径创建和帐户,例如登录,密码,姓名等),并且能够在我创建新员工时创建新用户。
这是我的员工创建控制器:
public ActionResult Create()
{
ViewBag.CompanyList = _service.ListCompany();
ViewBag.SupervisorList = _service.ListSupervisor();
return View();
}
//
// POST: /Employee/Create
[HttpPost]
public ActionResult Create([Bind(Exclude = "Id")] Employee objEmployee, FormCollection form)
{
ViewBag.CompanyList = _service.ListCompany();
ViewBag.SupervisorList = _service.ListSupervisor();
objEmployee.CreatedDate = System.DateTime.Now;
objEmployee.UpdatedDate = System.DateTime.Now;
objEmployee.CompanyId = int.Parse(form["CompanyId"]);
objEmployee.Supervisor = form["Supervisor"];
if (_service.Create(objEmployee))
return new RedirectResult(Url.Action("Index"));
else
{
if (!_service.Validate(objEmployee))
return View();
else
return new RedirectResult(Url.Action("Index", "Error", new { Message = "Error Create Employee", NextPage = CRAWebSiteMVC.Properties.Resources.Employee + @"/" + CRAWebSiteMVC.Properties.Resources.Create }));
}
}
这是通常的方式,我通过普通表单auth创建帐户。注册:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
[...]
}
// If we got this far, something failed, redisplay form
return View(model);
}
如何通过员工创建面板创建帐户,并基本上用员工列表替换常用的用户列表?
答案 0 :(得分:1)
现在通常上下文在控制器中自动声明为db< - 。 它被声明为
private ApplicationDbContext db = new ApplicationDbContext();
但是在我的例子中的这个案例中说。 context
如果宣布了ApplicationDbContext,只需将context
更改为db
。
底部是同时创建User类和Employee类的示例。因此在向Employee类引用时向用户插入记录。但我想你现在就明白了。
请注意,我没有为密码添加加密。因为这是一个全新的问题。
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
if (!context.Users.Any(t => t.UserName == "admin@Konek.com"))
{
var user = new ApplicationUser { UserName = "admin@Konek.com", Email = "admin@Konek.com" };
userManager.Create(user, "Password1!");
context.Roles.AddOrUpdate(r => r.Name, new IdentityRole { Name = "Admin" });
context.Roles.AddOrUpdate(r => r.Name, new IdentityRole { Name = "Receptionist" });
context.Roles.AddOrUpdate(r => r.Name, new IdentityRole { Name = "Security" });
context.SaveChanges();
userManager.AddToRole(user.Id, "Admin");
Employee admingEmployee = new Employee
{
Age=25,
Citizenship="Filipino",
CivilStatus=CivilStatus.Single,
DateOfBirth=DateTime.Now,
EmailAddress="admin@Konek.com",
FirstName="Admin",
Gender=Gender.Male,
HomeAddress="None",
LandLine="531-5555",
LastName="Administrator",
MiddleName="admin",
MobileNumber="09275225222",
Photo = "*******",
PlaceofBirth="*****",
Password = "********",
Role=Role.Admin
};
context.Employees.Add(admingEmployee);
context.SaveChanges();