示例#1
0
 public async Task<IActionResult> Register(string username, string password,string email,string question,string answer)
 {
     var v = DB.Users.Where(x => x.UserName == username).SingleOrDefault();
     if (v == null)
     {
         var user = new User
         {
             UserName = username,
             Email = email,
             Answer=answer,
             Question=question,
             NewsCount = 0,
             AriticleCount = 0,
             PostedCount = 0,
         };
         await UserManager.CreateAsync(user, password);
         var result = await UserManager.AddToRoleAsync(user, "普通用户");
         if (result.Succeeded)
         {
             return RedirectToAction("Index", "Home");
         }
         else
         {
             return View("no");
         }
     }
     else
     {
         return View("该用户已经存在,请重新更换用户名");
     }
     
 }
示例#2
0
        public IActionResult Create(string commentText, string RedirectUrl)
        {
            int?nullableDiscussonId = null;

            if (TempData["DiscussionId"] != null)
            {
                nullableDiscussonId = TempData["DiscussionId"] as int?;
            }
            int discussionId = nullableDiscussonId ?? 0;

            if (commentText != null)
            {
                var userId             = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
                Forum.Models.User user = this.dataManager.usersRepository.GetUserById(userId);



                Message message = new Message();
                message.Date         = DateTime.Now;
                message.Text         = commentText;
                message.UserId       = userId;
                message.User         = user;
                message.DiscussionId = discussionId;
                message.Discussion   = dataManager.discussionsRepository.GetDiscussionById(discussionId);
                dataManager.messagessRepository.SaveMessage(message);
            }
            return(Redirect(RedirectUrl));
        }
示例#3
0
        public IActionResult Create(Discussion discussion, string returnUrl)
        {
            int?nullableTopicId = null;

            if (TempData["TopicId"] != null)
            {
                nullableTopicId = TempData["TopicId"] as int?;
            }
            int topicId = nullableTopicId ?? 0;

            if (this.ModelState.IsValid)
            {
                var userId             = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
                Forum.Models.User user = this.dataManager.usersRepository.GetUserById(userId);


                discussion.Id      = 0;
                discussion.Date    = DateTime.Now;
                discussion.UserId  = userId;
                discussion.User    = user;
                discussion.TopicId = topicId;
                discussion.Topic   = dataManager.topicsRepository.GetTopicById(topicId);
                dataManager.discussionsRepository.SaveDiscussion(discussion);
            }
            if (returnUrl != null)
            {
                return(Redirect(returnUrl));
            }
            else
            {
                return(Redirect($"/Discussions/Index/{topicId}"));
            }
        }
示例#4
0
        public async static Task InitDB(IServiceProvider service)
        {
            var db = service.GetRequiredService<ForumContext>();

            var userManager = service.GetRequiredService<UserManager<User>>();

            var roleManager = service.GetRequiredService<RoleManager<IdentityRole>>();

            if (db.Database != null && db.Database.EnsureCreated())
            {
                await roleManager.CreateAsync(new IdentityRole { Name = "网站站长" });
                await roleManager.CreateAsync(new IdentityRole { Name = "网站管理员" });
                await roleManager.CreateAsync(new IdentityRole { Name = "普通用户" });

                var user = new User { UserName = "******" ,Name="付东东",Question="我最喜欢的编程语言",Answer="C#",NewsCount=0,AriticleCount=0,PostedCount=0};
                await userManager.CreateAsync(user, "Cream2015!@#");
                await userManager.AddToRoleAsync(user, "网站站长");

                var user1 = new User { UserName = "******" ,Name = "付东东" , Question = "我最喜欢的编程语言", Answer = "C#" , NewsCount = 0, AriticleCount = 0, PostedCount = 0 };
                await userManager.CreateAsync(user1, "Cream2015!@#");
                await userManager.AddToRoleAsync(user1, "网站管理员");

                var user2 = new User { UserName = "******" ,Name = "付东东", Question = "我最喜欢的编程语言", Answer = "C#", NewsCount = 0, AriticleCount = 0, PostedCount = 0 };
                await userManager.CreateAsync(user2, "Cream2015!@#");
                await userManager.AddToRoleAsync(user2, "普通用户");
            }
            db.SaveChanges();
        }
 public ActionResult Edit(User user)
 {
     if (ModelState.IsValid)
     {
         db.Entry(user).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(user);
 }
        public ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(user);
        }
 public static UserModel Parse(User currentUser)
 {
     return new UserModel()
     {
         AuthCode = currentUser.AuthCode,
         CreationDate = currentUser.CreationDate,
         IsAdmin = currentUser.IsAdmin,
         Id = currentUser.Id,
         SessionKey = currentUser.SessionKey,
         Username = currentUser.Username,
         IsBanned = currentUser.IsBanned
     };
 }