Beispiel #1
0
        public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs args)
        {
            HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];

            if (authCookie != null)
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
                string[] userDataTokens          = ticket.UserData.Split(new string[] { ";;" }, StringSplitOptions.RemoveEmptyEntries);

                LaunchKeyIdentity identity = new LaunchKeyIdentity(ticket.Name, userDataTokens[0], userDataTokens[1]);

                // verify user hasn't de-orbited
                var lkClient     = LaunchKeyClientFactory.GetInstanceFromConfig();
                var pollResponse = lkClient.Poll(identity.AuthRequest);

                if (lkClient.IsAuthorized(identity.AuthRequest, pollResponse))
                {
                    args.User = new GenericPrincipal(identity, null);
                }
                else
                {
                    // unset cookie
                    FormsAuthentication.SignOut();
                    lkClient.Logout(identity.AuthRequest);
                    Context.Response.Redirect("~");
                }
            }
        }
Beispiel #2
0
        public AuthsResponse Login(string username)
        {
            var lkClient      = LaunchKeyClientFactory.GetInstanceFromConfig();
            var authsResponse = lkClient.Authenticate(username, AuthenticationType.Session);

            return(authsResponse);
        }
Beispiel #3
0
        public object LoginPoll(string authRequest)
        {
            var lkClient     = LaunchKeyClientFactory.GetInstanceFromConfig();
            var pollResponse = lkClient.Poll(authRequest);

            // request failed for some reason, let client error out
            if (!pollResponse.Successful)
            {
                return(new { Successful = false, Waiting = false, ErrorCode = pollResponse.MessageCode, ErrorMessage = pollResponse.Message });
            }

            // request succeeded but still waiting
            if (pollResponse.UserHash == null)
            {
                return(new { Successful = true, Waiting = true });
            }

            // request succeeded, device responded with an OK
            if (pollResponse.DecryptedAuth.Response)
            {
                return(new { Successful = true, Waiting = false, Accepted = true, RedirectUrl = "LoginConfirm.aspx?authRequest=" + authRequest });
            }
            // request succeeded, device rejected
            else
            {
                lkClient.Logs(LogsAction.Authenticate, LogsStatus.Denied, authRequest);
                return(new { Successful = true, Waiting = false, Accepted = false });
            }
        }
Beispiel #4
0
        public ActionResult LoginJson(string username)
        {
            var lkClient      = LaunchKeyClientFactory.GetInstanceFromConfig();
            var authsResponse = lkClient.Authenticate(username, AuthenticationType.Session);

            return(Json(authsResponse, JsonRequestBehavior.AllowGet));
        }
Beispiel #5
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());
            }
        }
Beispiel #6
0
        protected void Page_Load(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            if (User != null)
            {
                var lkIdentity = User.Identity as LaunchKeyIdentity;
                if (lkIdentity != null)
                {
                    var lkClient = LaunchKeyClientFactory.GetInstanceFromConfig();
                    lkClient.Logout(lkIdentity.AuthRequest);
                }
            }

            Response.Redirect("~/");
        }
Beispiel #7
0
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var authRequest  = AuthRequest.Value;
                var lkClient     = LaunchKeyClientFactory.GetInstanceFromConfig();
                var pollResponse = lkClient.Poll(authRequest);

                if (lkClient.IsAuthorized(authRequest, pollResponse))
                {
                    this.SetAuthCookie(pollResponse.UserHash, authRequest, FriendlyName.Text);
                    Response.Redirect("~");
                }
            }
        }
Beispiel #8
0
        public ActionResult LogOff()
        {
            // logout locally
            FormsAuthentication.SignOut();

            var lkIdentity = User != null ? User.Identity as LaunchKeyIdentity : null;

            if (lkIdentity != null)
            {
                // notify launchKey
                var lkClient = LaunchKeyClientFactory.GetInstanceFromConfig();
                lkClient.Logout(lkIdentity.AuthRequest);
            }

            return(RedirectToAction("Index", "Home"));
        }
Beispiel #9
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 }));
            }
        }