Ejemplo n.º 1
0
        //[ValidateAntiForgeryToken]
        public ActionResult Signup(SignupViewModel model, string returnUrl)
        {
            //if (!ModelState.IsValid)
            //{
            //    return View(model);
            //}
            try
            {
                if (model.Password != model.ConfirmPassword)
                {
                    ModelState.AddModelError("", "注册不合法!");
                }
                EFCodeFirstDbContext context = new EFCodeFirstDbContext();

                Consultant user = new Consultant()
                {
                    Consultant_Name = model.Name,
                    PassWord        = model.Password,
                    Country         = model.Country,
                    Role            = model.Role
                };
                context.Consultants.Add(user);
                context.SaveChanges();
                return(RedirectToLocal(returnUrl));
            }
            catch
            {
                ModelState.AddModelError("", "注册不合法!");
                return(View(model));
            }
        }
Ejemplo n.º 2
0
        //[ValidateAntiForgeryToken]
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            EFCodeFirstDbContext context = new EFCodeFirstDbContext();
            var valid_users = context.Consultants.Select(x => x).Where(user => user.Consultant_Name == model.Name && user.PassWord == model.Password).FirstOrDefault();

            ViewData["Users"] = valid_users;
            //var login_user = valid_users.FirstOrDefault(x => model.Name.Equals(x.Account) && model.Password.Equals(x.Password));
            if (valid_users != null)
            {
                var claims = new List <Claim>();
                claims.Add(new Claim(ClaimTypes.NameIdentifier, valid_users.Consultant_Name));
                claims.Add(new Claim(ClaimTypes.Name, valid_users.PassWord));

                var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

                AuthenticationManager.SignIn(new AuthenticationProperties()
                {
                    AllowRefresh = true,
                    IsPersistent = model.RememberMe,
                    ExpiresUtc   = DateTime.UtcNow.AddMonths(1)
                }, identity);

                return(RedirectToLocal(returnUrl));
            }
            else
            {
                ModelState.AddModelError("", "登录不合法!");
                return(View(model));
            }
        }
Ejemplo n.º 3
0
        public ActionResult Signup(string returnUrl)
        {
            EFCodeFirstDbContext context = new EFCodeFirstDbContext();

            ViewBag.Countries = context.Country.Select(x => x.CountryName).ToList();
            //ViewBag.Roles = context.Role.Select(x => x).ToList();
            ViewBag.Roles = new List <string> {
                "Manager", "Consultant", "Dev"
            };
            ViewBag.ReturnUrl = returnUrl;
            return(View());
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var person = new Person
            {
                FirstName            = "Cui",
                LastName             = "YanWei",
                SocialSecurityNumber = 12345678
            };

            //新增一条记录,保存到数据库中
            using (var con = new EFCodeFirstDbContext())
            {
                con.Persons.Add(person);
                con.SaveChanges();
            }
            var firContext = new EFCodeFirstDbContext();
            //取第一条记录,并修改一个字段:这里是修改了SocialSecurityNumber=123
            //先不保存
            var p1 = firContext.Persons.FirstOrDefault();

            p1.SocialSecurityNumber = 123;
            //再创建一个Context,同样取第一条记录,修改SocialSecurityNumber=456并保存
            using (var secContext = new EFCodeFirstDbContext())
            {
                var p2 = secContext.Persons.FirstOrDefault();

                p2.SocialSecurityNumber = 456;
                secContext.SaveChanges();
            }
            try
            {
                firContext.SaveChanges();
                Console.WriteLine(" 保存成功");
            }
            catch (DbUpdateConcurrencyException ex)
            {
                Console.WriteLine(ex.Entries.First().Entity.GetType().Name + " 保存失败");
            }
            Console.Read();
        }