I followed the samples from ESRI (e.g. this one from GitHub) to enable feature-access to my service and everything worked fine. Next I changed the URL into my own feature-service and provided my crendetials to access that service.
So I ended up with the following code:
<SPAN class="comment token"><SPAN>//var serviceUrl = "</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fsampleserver6.arcgisonline.com%2Farcgis%2Frest%2Fservices%2FDamageAssessment%2FFeatureServer%2F0" target="_blank">http://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0</A><SPAN>";</SPAN></SPAN>
<SPAN class="keyword token">var</SPAN> serviceUrl <SPAN class="operator token">=</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fmyhost%2Farcgis%2Frest%2Fservices%2FMyService%2FFeatureServer%2F0" target="_blank">https://myhost/arcgis/rest/services/MyService/FeatureServer/0</A><SPAN>"</SPAN></SPAN><SPAN class="punctuation token">;</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>
ServerUri <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Uri</SPAN><SPAN class="punctuation token">(</SPAN>serviceUrl<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
TokenAuthenticationType <SPAN class="operator token">=</SPAN> TokenAuthenticationType<SPAN class="punctuation token">.</SPAN>ArcGISToken<SPAN class="punctuation token">,</SPAN>
TokenServiceUri <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Uri</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fmyhost%2Farcgis%2Fadmin%2FgenerateToken" target="_blank">https://myhost/arcgis/admin/generateToken</A><SPAN>"</SPAN></SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</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>
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">ChallengeHandler</SPAN><SPAN class="punctuation token">(</SPAN>
<SPAN class="keyword token">async</SPAN> info <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN>
<SPAN class="keyword token">await</SPAN> AuthenticationManager<SPAN class="punctuation token">.</SPAN>Current<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GenerateCredentialAsync</SPAN><SPAN class="punctuation token">(</SPAN>info<SPAN class="punctuation token">.</SPAN>ServiceUri<SPAN class="punctuation token">,</SPAN> user<SPAN class="punctuation token">,</SPAN> password<SPAN class="punctuation token">,</SPAN>
info<SPAN class="punctuation token">.</SPAN>GenerateTokenOptions<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">var</SPAN> featureTableUri <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">System<SPAN class="punctuation token">.</SPAN>Uri</SPAN><SPAN class="punctuation token">(</SPAN>serviceUrl<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">var</SPAN> table <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">ServiceFeatureTable</SPAN><SPAN class="punctuation token">(</SPAN>featureTableUri<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">var</SPAN> queryParameters <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">QueryParameters</SPAN> <SPAN class="punctuation token">{</SPAN> WhereClause <SPAN class="operator token">=</SPAN> <SPAN class="string token">"1 = 1"</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</SPAN>
FeatureQueryResult features <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">await</SPAN> table<SPAN class="punctuation token">.</SPAN><SPAN class="token function">QueryFeaturesAsync</SPAN><SPAN class="punctuation token">(</SPAN>queryParameters<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>When I execute this code I get the following exception on the last line, which doesn´t seem very meaningful to me:
Exception thrown<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'System.InvalidOperationException'</SPAN> <SPAN class="keyword token">in</SPAN> mscorlib<SPAN class="punctuation token">.</SPAN>dll
Additional information<SPAN class="punctuation token">:</SPAN> Cannot call <SPAN class="keyword token">this</SPAN> method <SPAN class="keyword token">in</SPAN> <SPAN class="keyword token">this</SPAN> context<SPAN class="punctuation token">:</SPAN> Object failed to load<SPAN class="punctuation token">,</SPAN> unable to execute task<SPAN class="punctuation token">.</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
When I set a breakpoint on the GenerateCredentialAsync-line (line 12) I also notice it´s never hit. So I wonder if the challengehandler is even registered correctly and fires the challenge-event appropriately.