protected void lnbSaveMessage_Click(object sender, EventArgs e)
        {
            using (ChatEntities context = new ChatEntities())
            {
                Message message = new Message();
                context.Messages.Add(message);

                message.Content = this.tbMessageContent.Text;
                message.PostDate = DateTime.Now;

                string firstName = (User.Identity.Name.Split(' '))[0];
                string lastName = (User.Identity.Name.Split(' '))[1];

                message.UserId = context.UserDetails.
                    Where(name => name.FirstName == firstName && name.LastName == lastName).
                    Select(x => x.UserId).FirstOrDefault();

                try
                {
                    context.SaveChanges();
                    ErrorSuccessNotifier.AddInfoMessage("Message created.");
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }
 public void grvMessages_DeleteItem(int messageId)
 {
     ChatEntities context = new ChatEntities();
     Message message = context.Messages.
         FirstOrDefault(q => q.MessageId == messageId);
     try
     {
         context.Messages.Remove(message);
         context.SaveChanges();
         this.grvMessages.PageIndex = 0;
         ErrorSuccessNotifier.AddInfoMessage("Message successfully deleted.");
     }
     catch (Exception ex)
     {
         ErrorSuccessNotifier.AddErrorMessage(ex);
     }
 }
        protected void CreateUser_Click(object sender, EventArgs e)
        {            
            string firstName = FirstName.Text;
            string lastName = LastName.Text;
            string userName = firstName + " " + lastName;
            string password = Password.Text;
            string email = Email.Text;
            bool admin = Admin.Checked;
            bool moderator = Moderator.Checked;

            var manager = new AuthenticationIdentityManager(new IdentityStore());
            User u = new User(userName) 
            { 
                UserName = userName
            };
            IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
            if (result.Success) 
            {
                using (ChatEntities ce = new ChatEntities())
                {
                    ce.UserDetails.Add(new UserDetail()
                    {
                        UserId = u.Id,
                        FirstName = firstName,
                        LastName = lastName,
                        Email = email,
                        Admin = admin,
                        Moderator = moderator
                    });

                    ce.SaveChanges();
                }
                if (admin)
                {
                    // Add Administrator.
                    if (!Roles.RoleExists("Admin"))
                    {
                        Roles.CreateRole("Admin");
                    }
                    
                    Membership.CreateUser(userName, password, email);
                    Roles.AddUserToRole(userName, "Admin");
                }
                else if (moderator)
                {
                    // Add Moderator
                    if (!Roles.RoleExists("Moderator"))
                    {
                        Roles.CreateRole("Moderator");
                    }

                    Membership.CreateUser(userName, password, email);
                    Roles.AddUserToRole(userName, "Moderator");
                }
                
                manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
                OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else 
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }
        }