How I can skip Optimizely authentication locally?

  • Page Owner: Not Set
  • Last Reviewed: 2023-07-20

I have a site that needs fixed immediately, but I don't have a login. I just need to be able to login locally, without going through the process to get an official account with the client. Is it possible, for a locally running site, to bypass authentication?


Answer

Can you? Yes. Should no? Probably not.

For framework ASP.NET sites, using Identity auth, you can add the following action to a controller. Hit the endpoint, and you will be logged in as "blend", in the groups listed below.

DO NOT COMMIT THIS:

        public ActionResult SkipAuth()
        {
            var authenticationManager = HttpContext.GetOwinContext().Authentication;

            var identity = new ClaimsIdentity(WsFederationAuthenticationDefaults.AuthenticationType);
            identity.AddClaim(new Claim(ClaimTypes.Name, "blend"));
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "blend"));
            identity.AddClaim(new Claim(ClaimTypes.GivenName, "Blend"));
            identity.AddClaim(new Claim(ClaimTypes.Role, "Everyone"));
            identity.AddClaim(new Claim(ClaimTypes.Role, "WebAdmins"));
            identity.AddClaim(new Claim(ClaimTypes.Role, "WebEditors"));

            authenticationManager.SignIn(new AuthenticationProperties
            {
                ExpiresUtc = DateTime.UtcNow.AddDays(5),
                IsPersistent = true
            }, identity);

            return Redirect("/");
        }

Note: If you're not using WsFederation, you may need to switch the ClaimsIdentity authentication type to the authentication type you are using. I think it would be something like: DefaultAuthenticationTypes.ApplicationCookie.


The following seems to work for CMS 12 out-of-the-box. Put this in your startup class:

            app.Map("/skip-auth", skip =>
            {
                skip.Use(async (HttpContext req, RequestDelegate next) =>
                {
                    var claims = new List<Claim>
                    {
                        new Claim(ClaimTypes.Name, "blend"),
                        new Claim(ClaimTypes.NameIdentifier, "blend"),
                        new Claim(ClaimTypes.GivenName, "Blend"),
                        new Claim(ClaimTypes.Role, "Everyone"),
                        new Claim(ClaimTypes.Role, "WebAdmins"),
                        new Claim(ClaimTypes.Role, "WebEditors")
                    };

                    var identity = new ClaimsIdentity(claims, "Identity.Application");
                    var principal = new ClaimsPrincipal(identity);

                    await req.SignInAsync(
                        "Identity.Application",
                        principal,
                        new AuthenticationProperties
                        {
                            ExpiresUtc = DateTime.UtcNow.AddDays(5),
                            IsPersistent = true
                        });

                    req.Response.Redirect("/");

                    return;
                });
            });