Example #1
0
        public ActionResult LoginNewUser(LoginNewUserModel model)
        {
            if (ModelState.IsValid)
            {
                var lkClient     = LaunchKeyClientFactory.GetInstanceFromConfig();
                var pollResponse = lkClient.Poll(model.AuthRequest);

                if (!lkClient.IsAuthorized(model.AuthRequest, pollResponse))
                {
                    ModelState.AddModelError(string.Empty, string.Format("Error communicating with LaunchKey. Response code: {0}, message: {1}", pollResponse.MessageCode, pollResponse.MessageCode));
                    return(View());
                }

                // create new user and login
                var db      = new LkExampleDatabaseDataContext();
                var newUser = new User {
                    FirstName = model.FriendlyName, LastAuthRequest = model.AuthRequest, LaunchKeyUserHash = pollResponse.UserHash
                };
                db.Users.InsertOnSubmit(newUser);
                db.SubmitChanges();

                this.SetAuthCookie(newUser);
                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                return(View());
            }
        }
Example #2
0
        public ActionResult LoginConfirm(string authRequest)
        {
            // confirm successful request.
            // Check hash against user database
            // If hash exists, login to that user
            // If hash not exists, redirect to confirm details view

            var lkClient = LaunchKeyClientFactory.GetInstanceFromConfig();

            // verify authenticity of auth request
            var pollResponse = lkClient.Poll(authRequest);

            if (lkClient.IsAuthorized(authRequest, pollResponse))
            {
                var db = new LkExampleDatabaseDataContext();

                // auth success, let's check if we know this person
                var currentUser = db.Users.Where(u => u.LaunchKeyUserHash == pollResponse.UserHash).FirstOrDefault();

                // we do, so set their auth cookie and send them back to the home page
                if (currentUser != null)
                {
                    currentUser.LastAuthRequest = authRequest;
                    db.SubmitChanges();
                    this.SetAuthCookie(currentUser);
                    return(RedirectToAction("Index", "Home"));
                }
                // unknown user. serve them the new user form
                else
                {
                    return(View("LoginNewUser", new LoginNewUserModel {
                        AuthRequest = authRequest
                    }));
                }
            }
            else
            {
                // show login error, send back to Login()
                return(View("Login", new { Error = true, ErrorMessage = pollResponse.Message }));
            }
        }