本文整理汇总了C#中UserService.Add方法的典型用法代码示例。如果您正苦于以下问题:C# UserService.Add方法的具体用法?C# UserService.Add怎么用?C# UserService.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserService
的用法示例。
在下文中一共展示了UserService.Add方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddUser
TestResult AddUser()
{
using (var unitOfWork = new UnitOfWork(new AuthorizationModuleFactory(true)))
{
var RoleService = new RoleService(unitOfWork);
var UserService = new UserService(unitOfWork);
var testUser = new User
{
Login = "ivan_test",
Password = "ivan_test_pass",
//Password = "f5DBIwnAMS",
Email = "[email protected]",
CreatedDate = DateTime.Now,
//FirstName = "Ivan",
//MiddleName = "Semenovich",
//LastName = "Peresvetov",
FirstName = "Иван",
MiddleName = "Семенович",
LastName = "Пересветов",
Roles = new List<Role>(new Role[] { RoleService.Get().First() }),
};
UserService.Add(testUser);
try
{
var result = unitOfWork.Commit();
if (result.Count > 0)
return new TestResult(TestResultType.Failure, MethodBase.GetCurrentMethod().Name, result.First().ErrorMessage);
}
catch (Exception ex)
{
while (ex.InnerException != null)
ex = ex.InnerException;
return new TestResult(TestResultType.Failure, MethodBase.GetCurrentMethod().Name, ex.Message);
}
}
using (var unitOfWork = new UnitOfWork(new AuthorizationModuleFactory(false)))
{
var UserService = new UserService(unitOfWork);
User testUser = UserService.Get(user => user.Login == "ivan_test").FirstOrDefault();
if(testUser == null)
return new TestResult(TestResultType.Failure, MethodBase.GetCurrentMethod().Name, "Can't find added user.");
else
return new TestResult(TestResultType.Success, MethodBase.GetCurrentMethod().Name, "User added successfully.");
}
}
示例2: Register
public ActionResult Register(RegisterViewModel register)
{
InterfaceUserService userService = new UserService();
if (TempData["VerficationCode"] == null || TempData["VerficationCode"].ToString() != register.VerificationCode.ToUpper())
{
ModelState.AddModelError("VerficationCode", "验证码不正确");
return View(register);
}
if (ModelState.IsValid)
{
if (userService.Exist(register.UserName)) ModelState.AddModelError("UserName", "用户名已存在");
else
{
User _user = new User()
{
UserName = register.UserName,
//默认用户组代码写这里
DisplayName = register.DisplayName,
Password = Security.Sha256(register.Password),
//邮箱验证与邮箱唯一性问题
Email = register.Email,
//用户状态问题
Status = 0,
LoginIP = "",
LoginTime = DateTime.Now,
RegistrationTime = DateTime.Now
};
_user = userService.Add(_user);
if (_user.UserID > 0)
{
var _identity = userService.CreateIdentity(_user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(_identity);
return RedirectToAction("Index", "Home");
}
else { ModelState.AddModelError("", "注册失败!"); }
}
}
return View(register);
}