public ActionResult AuthorizeExternalAccess()
        {
            //var pendingRequest = this.authorizationServer.ReadAuthorizationRequest(Request);

            var authorizationRequest = Session["AuthorizationRequest"] as OpenIdConnectAuthorizationRequest;

            if (authorizationRequest == null)
            {
                throw new HttpException((int)HttpStatusCode.BadRequest, "Missing authorization request.");
            }

            var requestingClient = MvcApplication.registeredUsers.FirstOrDefault(c => c.Email == User.Identity.Name);

            var model = new AlhambraOAuth2Authorization
            {
                UserId=User.Identity.Name,
                AuthorizedAt=DateTime.UtcNow,
                Scope = authorizationRequest.scope,
                AuthorizationRequest=authorizationRequest,
                State=authorizationRequest.state
            };

            MvcApplication.registeredAuthorizations.Add(model);

            return View(model);
        }
        public ActionResult AuthorizeExternalAccessResponse(bool isApproved)
        {
            var authorizationRequest = Session["AuthorizationRequest"] as OpenIdConnectAuthorizationRequest;

            if (authorizationRequest == null)
            {
                throw new HttpException((int)HttpStatusCode.BadRequest, "Missing authorization request.");
            }

            if (isApproved)
            {
                var client = MvcApplication.registeredUsers.FirstOrDefault(c => c.Email == User.Identity.Name);

                string newCode = GenerateHexEncodedGUI();
                //register the new code and set the 'used' flag as false
                MvcApplication.codesGenerated.Add(newCode, false);

                Guid newAccessToken = Guid.NewGuid();
                Guid newRefreshToken = Guid.NewGuid();

                MvcApplication.tokensGenerated.Add(newAccessToken, newRefreshToken);

                var model = new AlhambraOAuth2Authorization {
                    AccessToken=newAccessToken.ToString(),
                    RefreshToken=newRefreshToken.ToString(),
                    AuthorizationRequest=authorizationRequest ,
                    ExpiresAt = DateTime.Now.AddMinutes(2),
                    AuthorizedAt=DateTime.UtcNow,
                    Scope = authorizationRequest.scope,
                    UserId = client.Email,
                    Code = newCode,
                    State = authorizationRequest.state };

                var account = MvcApplication.registeredAuthorizations.FirstOrDefault(x =>  x.UserId == User.Identity.Name);
                //update existent info
                account.AccessToken = newAccessToken.ToString();
                account.RefreshToken = newRefreshToken.ToString();
                account.Code = newCode;

                account.ExpiresAt= DateTime.Now.AddMinutes(2);
                account.AuthorizedAt = DateTime.UtcNow;

                string url = authorizationRequest.redirect_uri + "?code=" + model.Code + "&state=" + model.State;

                return Redirect(url.ToString());

            }
            else
            {

                throw new HttpException((int)HttpStatusCode.Unauthorized, "Missing authorization request.");
            }
        }