コード例 #1
0
        public async Task <IActionResult> Register(RegisterModel model, [FromServices] IGenPasswordHash genPassHash)
        {
            if (ModelState.IsValid)
            {
                Author user = await db.Authors.FirstOrDefaultAsync(u => (u.Nickname == model.Nickname || u.Email == model.Email));

                if (user == null)
                {
                    // добавляем пользователя в бд
                    string passwordHash = genPassHash.GenerateHash(model.Password);

                    db.Authors.Add(new Author {
                        Nickname = model.Nickname, Email = model.Email, PasswordHash = passwordHash
                    });
                    await db.SaveChangesAsync();

                    await Authenticate(model.Nickname, false); // аутентификация

                    return(RedirectToAction("GetIndex", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Пользователь с данным email или nickname уже существует");
                }
            }
            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> PostNewPost(Post post)
        {
            if (ModelState.IsValid)
            {
                post.Created    = DateTime.Now;
                post.LastEdited = post.Created;

                db.Posts.Add(post);
                await db.SaveChangesAsync();

                return(RedirectToRoute("GetIndex"));
            }
            else
            {
                return(RedirectToAction("ErrorMessage", "Utility", new { Message = "Some fields isn't valid", Action = "GetPostToCreate", Controller = "Content" }));
            }
        }