Hello! Maybe someone could help me.
I’m trying to develop a UWP app and show a WebMap from my enterprise portal. My map is made of Feature Service which consist of 2 layers and 5 tables related to each other.
So, I’ve taken a pars of code examples for authentication:
<SPAN class="comment token">// Register the server information with the AuthenticationManager.</SPAN>
ServerInfo serverInfo <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">ServerInfo</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Uri</SPAN><SPAN class="punctuation token">(</SPAN>PortalUrl<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="comment token">//ServerUri = new Uri(PortalUrl),</SPAN>
TokenAuthenticationType <SPAN class="operator token">=</SPAN> TokenAuthenticationType<SPAN class="punctuation token">.</SPAN>OAuthImplicit<SPAN class="punctuation token">,</SPAN>
OAuthClientInfo <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">OAuthClientInfo</SPAN><SPAN class="punctuation token">(</SPAN>clientId<SPAN class="punctuation token">:</SPAN> AppClientId<SPAN class="punctuation token">,</SPAN> redirectUri<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Uri</SPAN><SPAN class="punctuation token">(</SPAN>OAuthRedirectUrl<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="comment token">// If a client secret has been configured, set the authentication type to OAuthAuthorizationCode.</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="operator token">!</SPAN>String<SPAN class="punctuation token">.</SPAN><SPAN class="token function">IsNullOrEmpty</SPAN><SPAN class="punctuation token">(</SPAN>ClientSecret<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="comment token">// Use OAuthAuthorizationCode if you need a refresh token (and have specified a valid client secret).</SPAN>
serverInfo<SPAN class="punctuation token">.</SPAN>TokenAuthenticationType <SPAN class="operator token">=</SPAN> TokenAuthenticationType<SPAN class="punctuation token">.</SPAN>OAuthAuthorizationCode<SPAN class="punctuation token">;</SPAN>
serverInfo<SPAN class="punctuation token">.</SPAN>OAuthClientInfo<SPAN class="punctuation token">.</SPAN>ClientSecret <SPAN class="operator token">=</SPAN> ClientSecret<SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="comment token">// Register this server with AuthenticationManager.</SPAN>
AuthenticationManager<SPAN class="punctuation token">.</SPAN>Current<SPAN class="punctuation token">.</SPAN><SPAN class="token function">RegisterServer</SPAN><SPAN class="punctuation token">(</SPAN>serverInfo<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="comment token">// Use a function in this class to challenge for credentials.</SPAN>
AuthenticationManager<SPAN class="punctuation token">.</SPAN>Current<SPAN class="punctuation token">.</SPAN>ChallengeHandler <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">DefaultChallengeHandler</SPAN> <SPAN class="punctuation token">{</SPAN> AllowSaveCredentials <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">true</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
My “ClientSecret” is set so (!String.IsNullOrEmpty(ClientSecret)) is true.
But when I add a map to a MapView control. I have to authenticate myself more than once.
As I found out I am challenged the first time to get to the Portal and the second is to get to the ArcGis Server.
After successful authentications I have 2 credentials:
AuthenticationManager<SPAN class="punctuation token">.</SPAN>Current<SPAN class="punctuation token">.</SPAN>Credentials<SPAN class="punctuation token">.</SPAN><SPAN class="token function">ToList</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
Count = 2
[0]: {Esri.ArcGISRuntime.Security.ArcGISTokenCredential}
[1]: {Esri.ArcGISRuntime.Security.OAuthTokenCredential}
I don’t know why, but the second authentication not always leads to success. So, please, make it clear for me:
1.How can I challenge User for credential only once and give him an access to all resources on Portal and Server.
2.How to store credentials and reuse it after application’s restart.
Thanks!