Copied to clipboard

Flag this post as spam?

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


  • Steven Newstead 62 posts 103 karma points
    Jun 08, 2011 @ 11:33
    Steven Newstead
    0

    Logging in a Backend Umbraco User via API

    Hi all,

    Any help on this would be appreciated so much as it's got me totally stumped.

    Our client has requested we replace the standard Umbraco login with a Single Sign-on solution, the flow being:

     

    • User lands on /umbraco/login.aspx
    • The single-sign-on provider notices it is monitoring this page and takes the user off to it's own login before anything fires on /umbraco/login.aspx
    • If the user passes validation it will be passed back with a username for the logging in user
    • I can then log the user in programmitcally by updating their password to something random (as they will never user this password) and logging them in
    In theory this is quite simple, however here is what I am doing
    • Taken the code from the standard umbraco login code behind
    • Override the validate user method to do my single sign on stuff
    • Call the exactly the same code as if the user had clicked the login click button passing the username and password from the OnLoad()
    Now here is the funny thing, the user will login however the whole content tree is blank, there are no errors in the log and I'm not totally stumped.
    If anyone has any ideas, am I missing setting a cookie somewhere etc. then please shout, the reason I am so confused is that I am using the exact same code as Umbraco does, the only real difference is that I am calling the exact same code that the OnClick() event calls in the OnLoad() e,g, I've done this:
     private void LogUserIn(string userName, string userPassword)
            {
                if (Membership.Providers[UmbracoSettings.DefaultBackofficeProvider].ValidateUser(userName, userPassword))
                {
                    if (Membership.Providers[UmbracoSettings.DefaultBackofficeProvider] is ActiveDirectoryMembershipProvider)
                    {
                        this.ActiveDirectoryMapping(userName, Membership.Providers[UmbracoSettings.DefaultBackofficeProvider].GetUser(userName, false).Email);
                    }
                    User u = new User(userName);
                    BasePage.doLogin(u);
                    if (u.DefaultToLiveEditing)
                    {
                        int startNodeId = u.StartNodeId;
                        if (startNodeId == -1)
                        {
                            if (CMSNode.CountLeafNodes(-1, Document._objectType) <= 0)
                            {
                                throw new Exception("There's currently no content to edit. Please contact your system administrator");
                            }
                            startNodeId = new Document(CMSNode.TopMostNodeIds(Document._objectType).First<Guid>()).Id;
                        }
                        string url = string.Format("{0}/canvas.aspx?redir=/{1}.aspx", SystemDirectories.Umbraco, startNodeId);
                        base.Response.Redirect(url, true);
                    }
                    if (this.hf_height.Value != "undefined")
                    {
                        this.Session["windowHeight"] = this.hf_height.Value;
                        this.Session["windowWidth"] = this.hf_width.Value;
                    }
                    if (string.IsNullOrEmpty(base.Request["redir"]))
                    {
                        base.Response.Redirect("umbraco.aspx");
                    }
                    else
                    {
                        base.Response.Redirect(base.Request["redir"]);
                    }
                }
                else
                {
                    this.loginError.Visible = true;
                }
            }

      protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                this.ClientLoader.DataBind();
                if (Membership.Providers["MyMembershipProvider"].ValidateUser("", ""))
                {
                    var userFound = false;
                    var userId = -1;
                    var password = string.Empty;
                    //todo there should be no need to do this but it is temp measure as a test
                    if (Session["TempSessionUserId"] != null && Session["TempSessionPassword"] != null)
                    {
                        userFound = Int32.TryParse(Session["TempSessionUserId"].ToString(), out userId);
                        password = Session["TempSessionPassword"].ToString();
                        if (userFound)
                        {
                            User u = new User(userId);
                            LogUserIn(u.LoginName, password);
                        }
                    }
    Thanks in advance for any help, it's driving me insane.

     

  • Bex 444 posts 555 karma points
    Jun 08, 2011 @ 12:00
    Bex
    0

    Hi Steven!

    I have just been scouring through my old projects as I knew I'd done this somewhere...

    I am doing it in a slightly different way to you but the only difference of the main part of code that I can see is that I am using 

     User u = new User(userName);
     doLogin(u);

    rather than your 

     

     User u = new User(userId);
     LogUserIn(u.LoginName, password);

     

     

     

     

     

    and doing a

     

      Response.Redirect("umbraco.aspx");

    Does this help? Bex EDIT: I should point out I am not doing any of my code on the login page of umbraco.. I am doing it on a completely separate page that my external site posts to.

     

  • Steven Newstead 62 posts 103 karma points
    Jun 08, 2011 @ 13:05
    Steven Newstead
    0

    Hi Bex,

    Thanks for the quick reply, this doesn't appear to be fixing the problem, curiously it looks like everything is logging in okay as I can see all the other tabs (such as users, documnet types etc.) but the main content isn't expanding (see here http://yfrog.com/h3ate9p) Have you or anyone seen this before? Maybe this is a seperate question....

    Thanks,

    Steve

  • Bex 444 posts 555 karma points
    Jun 08, 2011 @ 13:10
    Bex
    0

    I don't remember seeing that before...

    Clutching at straws here.. but maybe it has something to do with the start node for the user?

    Other than that I'm sorry but I'm out of ideas!

  • Steven Newstead 62 posts 103 karma points
    Jun 08, 2011 @ 14:49
    Steven Newstead
    0

    Thanks a lot for the ideas, it is a mystery. I have asked the above in the extending umbraco and will post back if / when I find the answer...

  • Steven Newstead 62 posts 103 karma points
    Jun 13, 2011 @ 15:49
    Steven Newstead
    0

    Ok, the solution was a little frustring...

    The sign on functionaility instructions that I was using wanted the web.config to change the authentication mode from:

    <authentication mode="Forms">

    to 

    <authentication mode="None">

    This allowed me to login but sent the treebuilder for the menus into a bit of a panic. I changed it back and it works fine. Not something most people would ever run into but it's worth knowing I suppose...

Please Sign in or register to post replies

Write your reply to:

Draft