Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • JJ 41 posts 83 karma points
    Nov 21, 2014 @ 13:58
    JJ
    0

    Backend login API

    Is there an API or programatic way to log administrators into the back end automatically?

    My scenario is that there is a client wants to use single sign on (some wierd custom Java thing they developed ages ago) to log in to all their systems, Umbraco included.

    So the users essentially need to click a link on their SSO portal and be taken without a login prompt to the Umbraco backend.

    Cheers

  • jivan thapa 194 posts 681 karma points
    Nov 22, 2014 @ 22:41
    jivan thapa
    0

    Hi, JJ.

    You can try this one. This is just for a POC. I have tested on localhost, U7.1.8, and it works.

    // First create a Httphandler. The Httphandler is called by a client whenever the client clicks on a link. Lets say the client clicks on a link "http://localhost/umb.sso?ssoid=1234" It has one parameter called ssoid. This id can be use for a security check !!!!!.

    Add the Httphandlers in your web.config file

    enter image description here

    Finally the HttpHandler looks like this one.

    namespace UmbracoTest.UmbHelper
    {
        public class UmbracoLoginHandler : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
    
                var ssoToken = string.IsNullOrEmpty(context.Request.Params["ssoId"])  ? "" : context.Request.Params["ssoId"];
    
                // verifty that the token 
                // !!!!!!!!!!!!!!!!!
                if (!ssoToken.Equals("1234")) return;
    
                // if everything is fine, lets login as admin
    
                var wrapper = new HttpContextWrapper(HttpContext.Current);
                var webSecurity = new WebSecurity(wrapper, ApplicationContext.Current);
                webSecurity.PerformLogin(0);
    
                // finally redirect to the umb backend. 
                HttpContext.Current.Response.Redirect( "/umbraco/");
            }
    
            public bool IsReusable {  get { return false; } }
        }
    }
    
  • Matt Barlow | jacker.io 164 posts 740 karma points c-trib
    Oct 27, 2015 @ 15:52
    Matt Barlow | jacker.io
    0

    Can also get the current User and log them into the backend automatically (windows auth).

      public void ProcessRequest(HttpContext context)
        {   
            var wrapper = new HttpContextWrapper(HttpContext.Current);
            var webSecurity = new WebSecurity(wrapper, ApplicationContext.Current);
            var userService = ApplicationContext.Current.Services.UserService;
            var userId = userService.GetByUsername(HttpContext.Current.User.Identity.Name);
            if (userId != null)
            {
                webSecurity.PerformLogin(userId);
                HttpContext.Current.Response.Redirect("/umbraco/");
            }else
            {
                HttpContext.Current.Response.Redirect("/");
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft