Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArcGIS portal - login in code without prompt

I am trying to write a program that will login a user programatically into a ArcGIS portal.

Here is my scenario:

User logs in to application A, user clicks a link to the ArcGIS portal - I do not want them to have to login to the portal when they click that link because that have already logged into application A.

SO

I would like:

User logs in to application A, user clicks a button containing the portal link as an argument and redirects to application B. Application B logs the user into portal and redirects them with the link from application A - the user is redirected without being prompted to login.

The portal is using portal tier authentication and I am using javascript but I could also use .NET/C#

UPDATE:

My current solution looks like this:

    var url = "https://PORTAL_DOMAIN/portal/sharing/rest/generateToken";
    var redirect = "https://PORTAL_DOMAIN/portal/home/webmap/PLACE_I_WANT_TO_REDIRECT_TO";

    var params = {
        'username': "username",
        'password': "password",
        'client': "referer",
        'referer': redirect,
        'expiration': 60,
        'f': 'json'
    };

    $.post(url, params)
        .done(function (data) {
            var tokenHolder = JSON.parse(data);
            var token = tokenHolder.token;
            $('body').append(token);

            document.cookie("esri_auth", token);
            window.location = redirect;
        });

This code gets me a token from the rest service - I try to store it has a cookie but it doesn't persist.

I have also tried using a C# web request and a credential cache to generate the credentials but I didn't save the code I was using.

like image 678
Connor Williams Avatar asked Nov 25 '25 01:11

Connor Williams


1 Answers

SOLVED IT:

Okay, so my original post was not far off from what I needed. My missing link was the cookie formatting and properties.

Also its important to mention that you cannot run this locally but you have to has access to the portal server and it only works once published out.

IN THE CODE BELOW:

ENCODED COOKIE - is an URL encoded json object. I signed into my portal and just copied the cookie format (using chrome dev tools) and concatenated the generated token into the cookie and redirected. ALSO I had to set the domain,expire, and path properties of the cookie.

    var url = "https://PORTAL_DOMAIN/portal/sharing/rest/generateToken";

    var redirect = "https://PORTAL_DOMAIN/portal/home/webmap/PLACE_I_WANT_TO_REDIRECT_TO";

          var params = {
              'username': "username",
              'password': "password",
              'client': "referer",
              'referer': redirect,
              'expiration': 60,
              'f': 'json'
          };



        $.post(url, params)
            .done(function (data) {
                var tokenHolder = JSON.parse(data);
                var token = tokenHolder.token;
                var domain = ".PORTAL_DOMAIN";

                document.cookie = "esri_auth=ENCODED COOKIE;expires=Session;domain=" +domain + ";path=/;secure";

                window.location = redirect;
            });
like image 89
Connor Williams Avatar answered Nov 27 '25 14:11

Connor Williams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!