public async Task Update(TeacherModel teacher)
        {
            var store   = new Microsoft.AspNet.Identity.EntityFramework.UserStore <ApplicationUser>(new ApplicationDbContext());
            var manager = new UserManager <ApplicationUser>(store);
            var target  = manager.FindByEmail(teacher.Email);

            if (target != null)
            {
                target.Id             = teacher.UserID;
                target.FirstName      = teacher.FirstName;
                target.LastName       = teacher.LastName;
                target.HireDate       = teacher.HireDate;
                target.PhoneNumber    = teacher.PhoneNumber;
                target.Email          = teacher.Email;
                target.MNPSEmployeeNo = teacher.MNPSEmployeeNo;
                target.IsDeleted      = teacher.IsDeleted;
                target.EndDate        = teacher.EndDate;
                target.Volunteer      = teacher.Volunteer;
                target.MainSubject    = teacher.MainSubject;
                target.SubjectOther   = teacher.SubjectOther;
                target.GradeLevel     = teacher.GradeLevel;
            }
            await manager.UpdateAsync(target);

            var ctx = store.Context;

            ctx.SaveChanges();
        }
Ejemplo n.º 2
0
        public async System.Threading.Tasks.Task <ActionResult> Register(string username, string password)
        {
            var userStore = new Microsoft.AspNet.Identity.EntityFramework.UserStore <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>();
            var manager   = new Microsoft.AspNet.Identity.UserManager <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(userStore);
            var user      = new Microsoft.AspNet.Identity.EntityFramework.IdentityUser()
            {
                UserName = username
            };

            Microsoft.AspNet.Identity.IdentityResult result = await manager.CreateAsync(user, password);

            if (result.Succeeded)
            {
                //I have some options: log them in, or I can send them an email to "Confirm" their account details.'
                //I don't have email set up this week, so we'll come back to that.
                //This authentication manager will create a cookie for the current user, and that cookie will be exchanged on each request until the user logs out

                var authenticationManager = HttpContext.GetOwinContext().Authentication;
                var userIdentity          = await manager.CreateIdentityAsync(user, Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);

                authenticationManager.SignIn(new Microsoft.Owin.Security.AuthenticationProperties()
                {
                }, userIdentity);
            }
            else
            {
                ViewBag.Error = result.Errors;
                return(View());
            }
            return(RedirectToAction("Index", "Home"));
        }
Ejemplo n.º 3
0
        public async System.Threading.Tasks.Task <ActionResult> Register(string username, string email, string password)
        {
            var userStore = new Microsoft.AspNet.Identity.EntityFramework.UserStore <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>();
            var manager   = new Microsoft.AspNet.Identity.UserManager <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(userStore);
            var user      = new Microsoft.AspNet.Identity.EntityFramework.IdentityUser()
            {
                UserName = username, Email = email, EmailConfirmed = false
            };

            manager.UserTokenProvider =
                new Microsoft.AspNet.Identity.EmailTokenProvider <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>();

            Microsoft.AspNet.Identity.IdentityResult result = await manager.CreateAsync(user, password);

            if (result.Succeeded)
            {
                //I have some options: log them in, or I can send them an email to "Confirm" their account details.'
                //I don't have email set up this week, so we'll come back to that.
                string confirmationToken = await manager.GenerateEmailConfirmationTokenAsync(user.Id);

                string confirmationLink = Request.Url.GetLeftPart(UriPartial.Authority) + "/Account/Confirm/" + user.Id + "?token=" + confirmationToken;

                string apiKey = System.Configuration.ConfigurationManager.AppSettings["SendGrid.ApiKey"];

                SendGrid.ISendGridClient           client = new SendGrid.SendGridClient(apiKey);
                SendGrid.Helpers.Mail.EmailAddress from   = new SendGrid.Helpers.Mail.EmailAddress("*****@*****.**", "Coding Cookware Administrator");

                SendGrid.Helpers.Mail.EmailAddress to = new SendGrid.Helpers.Mail.EmailAddress(email);

                string subject = "Confirm your Coding Cookware Account";

                string htmlContent      = string.Format("<a href=\"{0}\">Confirm Your Account</a>", confirmationLink);
                string plainTextContent = confirmationLink;

                SendGrid.Helpers.Mail.SendGridMessage message = SendGrid.Helpers.Mail.MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);

                SendGrid.Response response = await client.SendEmailAsync(message);

                TempData["EmailAddress"] = email;

                return(RedirectToAction("ConfirmationSent"));


                //Commenting this out: I'm not going to log the user in on registration anymore - I'm going to send them a confirmation email instead.
                //This authentication manager will create a cookie for the current user, and that cookie will be exchanged on each request until the user logs out
                //var authenticationManager = HttpContext.GetOwinContext().Authentication;
                //var userIdentity = await manager.CreateIdentityAsync(user, Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);
                //authenticationManager.SignIn(new Microsoft.Owin.Security.AuthenticationProperties() { }, userIdentity);
            }
            else
            {
                ViewBag.Error = result.Errors;
                return(View());
            }

            return(RedirectToAction("Index", "Home"));
        }
Ejemplo n.º 4
0
 public ApplicationUser Initialize(string userName)
 {
     if (userName != null && User == null)
     {
         var store       = new Microsoft.AspNet.Identity.EntityFramework.UserStore <ApplicationUser>(new ApplicationDbContext());
         var userManager = new UserManager <ApplicationUser>(store);
         //ApplicationUser user = userManager.FindByNameAsync(User.Identity.Name).Result;
         //User = userManager.FindById(userId);
         User = userManager.FindByName(userName);
     }
     return(User);
 }
Ejemplo n.º 5
0
        public async System.Threading.Tasks.Task <ActionResult> LogOn(string username, string password, bool?staySignedIn, string returnUrl)
        {
            var userStore = new Microsoft.AspNet.Identity.EntityFramework.UserStore <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>();
            var manager   = new Microsoft.AspNet.Identity.UserManager <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(userStore);



            var user = await manager.FindByNameAsync(username);


            bool result = await manager.CheckPasswordAsync(user, password);

            if (result)
            {
                if (user.EmailConfirmed)
                {
                    //I have some options: log them in, or I can send them an email to "Confirm" their account details.'
                    //I don't have email set up this week, so we'll come back to that.

                    //This authentication manager will create a cookie for the current user, and that cookie will be exchanged on each request until the user logs out
                    var authenticationManager = HttpContext.GetOwinContext().Authentication;
                    var userIdentity          = await manager.CreateIdentityAsync(user, Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new Microsoft.Owin.Security.AuthenticationProperties()
                    {
                    }, userIdentity);
                }
                else
                {
                    ViewBag.Error = new string[] { "Your email address has not been confirmed." };
                    return(View());
                }
            }
            else
            {
                ViewBag.Error = new string[] { "Unable to Log In, check your username and password" };
                return(View());
            }
            if (string.IsNullOrEmpty(returnUrl))
            {
                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                return(Redirect(returnUrl));
            }
        }
Ejemplo n.º 6
0
        public async System.Threading.Tasks.Task <ActionResult> Confirm(string id, string token)
        {
            var userStore = new Microsoft.AspNet.Identity.EntityFramework.UserStore <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>();
            var manager   = new Microsoft.AspNet.Identity.UserManager <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(userStore);

            manager.UserTokenProvider =
                new Microsoft.AspNet.Identity.EmailTokenProvider <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>();


            var result = await manager.ConfirmEmailAsync(id, token);

            if (result.Succeeded)
            {
                TempData["Confirmed"] = true;
                return(RedirectToAction("LogOn"));
            }
            else
            {
                return(HttpNotFound());
            }
        }
Ejemplo n.º 7
0
        public ActionResult Index()
        {
            var store = new Microsoft.AspNet.Identity.EntityFramework.UserStore<NetCafeWeb.Models.ApplicationUser>(new NetCafeWeb.Models.ApplicationDbContext());
            var manager = new Microsoft.AspNet.Identity.UserManager<NetCafeWeb.Models.ApplicationUser>(store);

            var a = manager.IsInRoleAsync(User.Identity.GetUserId(), "Admin");
            bool isAdmin = a.Result;

            IRepository<User> userRepository = new UserRepository();
            IEnumerable<User> users = userRepository.List;
            ViewBag.users = users.Cast<User>().ToList();

            IRepository<PC> pcRepository = new PCRepository();
            IEnumerable<PC> pcs = pcRepository.List;
            ViewBag.pcs = pcs.Cast<PC>().ToList();

            if (isAdmin)
            {
                //show het order
                IRepository<Order> repository = new OrderRepository();
                IEnumerable<Order> order = repository.List;
                ViewBag.orders = order.Cast<Order>().ToList();
                //FormsAuthentication.SetAuthCookie("asd", false);
                return View();
            }
            else //La supervisor
            {
                //Lay supervior user
                String supervisorName = User.Identity.Name;
                //Lay supervior id
                UserRepository repo = new UserRepository();
                NetCafeRepository netRepo = new NetCafeRepository();
                OrderRepository orderRepo = new OrderRepository();
                int supervisorId = repo.getIDByUsername(supervisorName);
                //Lay netcafe id
                int netID = netRepo.getNetCafeIDByName(supervisorId);
                //hien thi order cua netcafe id
                List<Order> orders = orderRepo.getOrderByNetCafe(netID);
                ViewBag.orders = orders;
            }

            return View();
        }
Ejemplo n.º 8
0
        public ActionResult Index(int? id)
        {
            var store = new Microsoft.AspNet.Identity.EntityFramework.UserStore<NetCafeWeb.Models.ApplicationUser>(new NetCafeWeb.Models.ApplicationDbContext());
            var manager = new Microsoft.AspNet.Identity.UserManager<NetCafeWeb.Models.ApplicationUser>(store);
            var a = manager.IsInRoleAsync(User.Identity.GetUserId(), "Admin");
            //var b = manager.IsInRoleAsync(User.Identity.GetUserId(), "Supervisor");
            bool isAdmin = a.Result;
            a = manager.IsInRoleAsync(User.Identity.GetUserId(), "Supervisor");
            bool isSupervisor = a.Result;

            List<PC> PCList = new List<PC>();
            List<NetCafe> NetList = new List<NetCafe>();

            if (isSupervisor)
            {
                string username = User.Identity.Name;
                NetList = PCService.GetManageNet(username);
                int selectedNetID = 0;

                if (NetList != null && NetList.Count > 0)
                {
                    foreach (NetCafe netCafe in NetList)
                    {
                        PCList = PCService.FindByNetID(netCafe.NetCafeID);
                        selectedNetID = netCafe.NetCafeID;
                    }
                }

                var query = PCList.OrderBy(p => p.PCStatus).ThenBy(p => p.PCName).ThenBy(p => p.Price);
                ViewBag.PCList = query.ToList();
                ViewBag.NetList = NetList;
                ViewBag.Role = "Supervisor";
                return View();
            }
            else
            {
                if (id != null && id > 0)
                {
                    PCList = PCService.FindByNetID(id.Value);
                    ViewBag.pcList = PCList;
                    ViewBag.SelectedNetcafe = PCService.GetNetCafeByID(id.Value);
                }
                else
                {
                    PCList = PCService.GetPCList();
                    ViewBag.pcList = PCList;
                }

                var query = PCList.OrderBy(p => p.NetCafeID).ThenBy(p => p.PCStatus).ThenBy(p => p.PCName).ThenBy(p => p.Price);
                ViewBag.PCList = query.ToList();
                ViewBag.NetList = PCService.GetNetList();
                ViewBag.Role = "Admin";

                return View();
            }
        }
Ejemplo n.º 9
0
        public async Task <ActionResult> ParticipantList(HttpPostedFileBase ParticipantListFile, int CourseId = 0)
        {
            try
            {
                string ParticipantFilePath = Path.Combine(Server.MapPath("~/Upload"), Path.GetFileName(ParticipantListFile.FileName));

                ParticipantListFile.SaveAs(ParticipantFilePath);

                var _userStore   = new Microsoft.AspNet.Identity.EntityFramework.UserStore <ApplicationUser>(db);
                var _userManager = new UserManager <ApplicationUser>(_userStore);

                XDocument xdocFromFile = XDocument.Load(ParticipantFilePath);

                XElement studentXList = xdocFromFile.Root.Elements().ElementAt(4); //Excel 2003: ElementAt(3)

                if (studentXList != null &&
                    studentXList.FirstAttribute.Value.ToUpper() == "PARTICIPANTS" &&
                    studentXList.Elements().First().Elements().ElementAt(3).Elements().First().Value.ToUpper() == "USERFIRSTNAME" &&
                    studentXList.Elements().First().Elements().ElementAt(3).Elements().ElementAt(1).Value.ToUpper() == "USERLASTNAME" &&
                    studentXList.Elements().First().Elements().ElementAt(3).Elements().ElementAt(2).Value.ToUpper() == "EMAIL")
                {
                    ApplicationUser student = null;

                    for (var i = 4; i < studentXList.Elements().First().Elements().Count(); i++)
                    {
                        student = new ApplicationUser();

                        student.UserFirstName = studentXList.Elements().First().Elements().ElementAt(i).Elements().First().Value;
                        student.UserLastName  = studentXList.Elements().First().Elements().ElementAt(i).Elements().ElementAt(1).Value;
                        student.Email         = studentXList.Elements().First().Elements().ElementAt(i).Elements().ElementAt(2).Value;
                        student.UserName      = student.Email;
                        student.CourseId      = CourseId;
                        student.UserStartDate = DateTime.Now;

                        var result = await _userManager.CreateAsync(student, "leXicon");

                        if (result.Succeeded)
                        {
                            _userManager.AddToRole(student.Id, "Student");
                        }
                    }
                    System.IO.File.Delete(ParticipantFilePath);
                    return(RedirectToAction("Details", "Courses", new { Id = CourseId })); // Success!
                }
                else
                {
                    try
                    {
                        System.IO.File.Delete(ParticipantFilePath);
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Message = ex.Message;
                        return(View());
                    }
                    ViewBag.Message = "Uploaded file is not of the right standard for these accounts!";
                    return(View());
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
                return(View());
            }

            //return View();
        }
Ejemplo n.º 10
-1
        public static ApplicationUser GetApplicationUser()
        {
            ApplicationDbContext db = new ApplicationDbContext();
            var store = new Microsoft.AspNet.Identity.EntityFramework.UserStore<VSMR12.Models.ApplicationUser>(new VSMR12.Models.ApplicationDbContext());
            var userManager = new Microsoft.AspNet.Identity.UserManager<VSMR12.Models.ApplicationUser>(store);

            VSMR12.Models.ApplicationUser user = userManager.FindByNameAsync(System.Web.HttpContext.Current.User.Identity.Name).Result;

            return user;
        }
Ejemplo n.º 11
-1
        public static vResource GetResource()
        {
            ApplicationDbContext db = new ApplicationDbContext();
            var store = new Microsoft.AspNet.Identity.EntityFramework.UserStore<VSMR12.Models.ApplicationUser>(new VSMR12.Models.ApplicationDbContext());
            var userManager = new Microsoft.AspNet.Identity.UserManager<VSMR12.Models.ApplicationUser>(store);

            VSMR12.Models.ApplicationUser user = userManager.FindByNameAsync(System.Web.HttpContext.Current.User.Identity.Name).Result;

            vResource resource = db.vResource.Find(user.UserId);

            return resource;
        }