コード例 #1
0
ファイル: AuthController.cs プロジェクト: mchella33/practice
        public ActionResult Register(RegisterViewModel input) //'input' below is information gathered and entered in to the 'RegisterViewModel' once submitted
        {
            if (ModelState.IsValid)
            {
                using (var db = new MyWordEntities())
                {
                    var participant = db.Participants.FirstOrDefault(x => x.EmailId == input.EmailId);

                    if (participant == null && input.Password == input.RepeatPassword)
                    {
                        var newParticipant = new Participant();
                        newParticipant.ParticipantId = Guid.NewGuid();
                        newParticipant.EmailId       = input.EmailId;
                        newParticipant.FirstName     = input.FirstName;
                        newParticipant.LastName      = input.LastName;
                        newParticipant.DisplayName   = input.DisplayName;
                        newParticipant.Password      = input.Password;

                        db.Participants.Add(newParticipant);

                        db.SaveChanges();
                        var persistentCookie = input.RememberMe;
                        var ticket           = new FormsAuthenticationTicket(
                            1,
                            input.EmailId,
                            DateTime.Now,
                            DateTime.Now.AddMinutes(30),
                            persistentCookie,
                            newParticipant.ParticipantId.ToString()
                            );
                        var cookie = new HttpCookie(FormsAuthentication.FormsCookieName,
                                                    FormsAuthentication.Encrypt(ticket))
                        {
                            Path = FormsAuthentication.FormsCookiePath
                        };
                        if (persistentCookie)
                        {
                            cookie.Expires = ticket.Expiration;
                        }
                        Response.Cookies.Add(cookie);

                        //FormsAuthentication.SetAuthCookie(input.UserName, input.RememberMe);//adds authentication cookie
                        return(RedirectToAction("Index", "Home"));//sends them straight to 'home index'
                    }
                }//closing using statement destroys db connection
            }
            return(View(input));//if model state is invalid, returns to register and
        }
コード例 #2
0
ファイル: HomeController.cs プロジェクト: mchella33/practice
        public ActionResult Profile()
        {
            var         cookievalue = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value);
            ViewProfile viewModel;

            using (var db = new MyWordEntities())
            {
                var profile = db.Profiles.FirstOrDefault(x => x.ProfileId == new Guid(cookievalue.UserData));
                viewModel = new ViewProfile
                {
                    DisplayName = profile.DisplayName,
                    Comment     = profile.Comment,
                    Email       = profile.Email,
                };
            }
            return(View(viewModel));
        }
コード例 #3
0
ファイル: AuthController.cs プロジェクト: mchella33/practice
        public ActionResult Index(Participant input, string returnUrl = "")
        {
            if (ModelState.IsValid)
            {
                using (var db = new MyWordEntities())
                {
                    var participant =
                        db.Participants.FirstOrDefault(x => x.EmailId == input.EmailId && x.Password == input.Password);
                    if (participant != null)
                    {
                        var persistentCookie = true;

                        var ticket = new FormsAuthenticationTicket(
                            1,
                            input.EmailId,
                            DateTime.Now,
                            DateTime.Now.AddMinutes(30),
                            persistentCookie,
                            participant.ParticipantId.ToString()
                            );
                        var cookie = new HttpCookie(FormsAuthentication.FormsCookieName,
                                                    FormsAuthentication.Encrypt(ticket))
                        {
                            Path = FormsAuthentication.FormsCookiePath
                        };
                        if ((bool)persistentCookie)
                        {
                            cookie.Expires = ticket.Expiration;
                        }
                        Response.Cookies.Add(cookie);
                        //FormsAuthentication.SetAuthCookie(user.UserName, input.RememberMe);  removed this due to fixing auth

                        return(RedirectToAction("MainPage", "Home"));
                    }
                }
            }
            ModelState.Remove("Password"); //removes password when not authenticated
            return(View());
        }