Select to view content in your preferred language

Cannot generate a token programmly for a secure map service.

814
2
06-10-2011 02:35 PM
DanDong
Deactivated User
Hi everyone,

Currently the map services used in my project are not secured, so I want to secure them. I learned some information from ESRI blog that I need to first generate a token for the map service. So I wrote such codes in code-behind.

private void ArcGISDynamicMapServiceLayer_Initialized(object sender, EventArgs e)
        {
dynamicServiceLayer =
            sender as ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer;
if (dynamicServiceLayer.VisibleLayers == null)
                dynamicServiceLayer.VisibleLayers = GetDefaultVisibleLayers(dynamicServiceLayer);
ConfigureLayerWithToken(dynamicServiceLayer); 
        }




private void ConfigureLayerWithToken(ArcGISDynamicMapServiceLayer layer)
        {
            string tokenurl =
                string.Format("http://rmms-dev.atlas.illinois.edu/ArcGIS/Tokens?request=getToken&username={0}&password={1}&timeout={2}", 
                username, password, timeout);
            var client = new WebClient();
            client.DownloadStringCompleted += (sender, e) => { layer.Token = e.Result; };
            client.DownloadStringAsync(new Uri(tokenurl));           
        }


But there is an exception when I run this code. It says: An exception occurred during the operation, making the result invalid. I attach a screenshot for more details. [ATTACH]7273[/ATTACH]

Does anyone run into such issue? Or any ideas or thoughts on how to solve this? Thanks a lot!!
0 Kudos
2 Replies
JenniferNery
Esri Regular Contributor
Your code seem correct except for this line:
layer.Token = e.Result;
and I wonder if layer here has already been added to the map or initialized. You cannot add or initialize the layer if the service requires token.

It would help too if you posted the stack trace but I suspect it is because you are modifying Map.Layer property which was created at the UI thread. You can fix this by creating a static property Token and instead do Token = e.Result. When you are ready to create an instance of your layer, set layer.Token = Token.
0 Kudos
DanDong
Deactivated User
because you are modifying Map.Layer property which was created at the UI thread. You can fix this by creating a static property Token and instead do Token = e.Result. When you are ready to create an instance of your layer, set layer.Token = Token.


Hey Jennifer, thanks for the tips. I was testing this recently with many ways, but haven't get it work 😞

Let me describe more about my issue. I have several map services in http://rmms-dev.atlas.illinois.edu/ArcGIS/rest/services, like A, B, C and D (all of them currently are not secured.). I have three level users set in ArcGIS Server, such as i, j and k.
i, j and k can all use A and B service.
j and k can both use C service, but not for i.
only k can use D service.

So my questions are:
1. How should I set different security to different service? If I use different tokens, I should generate different tokens for each of four services. But how should I do to let all three levels users can use A and B, since a token is generated with single username&password?
2. I tired to use the SLProxyPage (http://help.arcgis.com/en/webapi/silverlight/help/index.html#/Secure_services/016600000022000000/). If I add the following lines:
<serverItem url="http://rmms-dev.atlas.illinois.edu/ArcGIS/rest/services"
                matchAll="true" username="i" password="iii"/>


And use it in xaml (This is service A)
<esri:ArcGISDynamicMapServiceLayer ID="Visible Layers"
                  Url="http://rmms-dev.atlas.illinois.edu/ArcGIS/rest/services/wirt/MapServer" 
                  Initialized="ArcGISDynamicMapServiceLayer_Initialized" InitializationFailed="AGS_failed" ProxyURL="../proxy.ashx" />

But that still means only user i can use service A, then how about user j and k?

Sorry...my questions are many...I am totally a brand new in this security issue....I really don't know how to do this...:( I only know in the beginning I need a logon window to get the user' name and passoword(maybe use a childwindow)
0 Kudos