<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Client Credentials REST Host</title><link>https://community.telligent.com/community/11/w/developer-training/65132/client-credentials-rest-host</link><description /><dc:language>en-US</dc:language><generator>14.0.0.586 14</generator><item><title>Client Credentials REST Host</title><link>https://community.telligent.com/community/11/w/developer-training/65132/client-credentials-rest-host</link><pubDate>Thu, 13 Jun 2019 19:32:46 GMT</pubDate><guid isPermaLink="false">ab3a02b8-ec8c-4e50-aa03-b538ffc2500a</guid><dc:creator>Ben Tiedt</dc:creator><comments>https://community.telligent.com/community/11/w/developer-training/65132/client-credentials-rest-host#comments</comments><description>Current Revision posted to Developer Training by Ben Tiedt on 06/13/2019 19:32:46&lt;br /&gt;
&lt;p&gt;Of the 2 hosts available&amp;nbsp;in the SDK, the Client Credentials REST Host is the most straight forward and easy to use. &amp;nbsp; It can be instantiated traditionally with the required information as constructor arguments or it can use an application configuration file to store information. &amp;nbsp;It is also the only shipped host that can be used outside of a web-based application. &amp;nbsp;The Client Credentials REST Host is primarily meant to be used for utility applications, not integrations.&lt;/p&gt;
&lt;p&gt;[toc]&lt;/p&gt;
&lt;h2&gt;&lt;a id="Setup" name="Setup"&gt;&lt;/a&gt;Setup&lt;/h2&gt;
&lt;p&gt;If you installed the SDK via NuGet then a communityserver_SDK.config file and an oauth.ashx file were installed. &amp;nbsp;If you only plan on using the Client Credentials REST Host then you can delete these files as they are not used.&lt;/p&gt;
&lt;p&gt;The Client Credentials REST Host requires you create an OAuth client in your community site. &amp;nbsp;This client should use the client credentials grant type and be a confidential client. &amp;nbsp;For more information on creating OAuth clients and using grant types see [[Authentication|REST API Authentication]]. &amp;nbsp;Record your client Id and client secret.&lt;/p&gt;
&lt;p&gt;This host requires 4 pieces of information to work:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;deafaultUsername&lt;/strong&gt;: The user that will be primarily used by the host when not impersonating&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;communityUrl&lt;/strong&gt;: The fully qualified domain name of your community&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;clientId&lt;/strong&gt;: The client ID of the Oauth client you created&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;clientSecret&lt;/strong&gt;: The Oauth client secret from the client you created.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You have 2 options for creating the host. &amp;nbsp;The first option is just to instantiate it like any other class passing in the required arguments above:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt;
    var host = new ClientCredentialsRestHost(&amp;quot;admin&amp;quot;
        ,&amp;quot;http://yourcommunityurl.com&amp;quot;
        , &amp;quot;[Your Client Id]&amp;quot;
        , &amp;quot;[Your Client Secret]&amp;quot;);
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The second is to use an application configuration file. &amp;nbsp;Locate your web.config or app.config file in your solution or add one if one does not exist. &amp;nbsp;If the &amp;lt;AppSettings /&amp;gt; node is not present add it and then add the following information using values you recored from your Oauth client setup:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="xml"&gt;&amp;lt;appSettings&amp;gt;
    &amp;lt;add key=&amp;quot;communityUrl&amp;quot; value=&amp;quot;[http://yourcommunityurl.com]&amp;quot; /&amp;gt;
    &amp;lt;add key=&amp;quot;defaultUsername&amp;quot; value=&amp;quot;admin&amp;quot; /&amp;gt;
    &amp;lt;add key=&amp;quot;clientId&amp;quot; value=&amp;quot;[Oauth Client Id]&amp;quot; /&amp;gt;
    &amp;lt;add key=&amp;quot;clientSecret&amp;quot; value=&amp;quot;[Oauth Client Secret]&amp;quot; /&amp;gt;
&amp;lt;/appSettings&amp;gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;You will get errors if you try to instantiate the host using its default constructor if you have not setup the values in configuration.&lt;/p&gt;
&lt;h2&gt;&lt;a id="Lifecycle" name="Lifecycle"&gt;&lt;/a&gt;Lifecycle&lt;/h2&gt;
&lt;p&gt;The Client Credentials REST Host is like any other class and survives based on the scope it was created in. &amp;nbsp; It is not expensive to create a Client Credentials REST Host however it is also not necessary to do so often. &amp;nbsp; Generally you only need to create this host once per application. &amp;nbsp;You can create multiple hosts if so needed for additional communities or alternate configurations, however only 1 host can use the application config to store values, all the others must use the constructor.&lt;/p&gt;
&lt;h2&gt;&lt;a id="Authentication_and_Impersonation" name="Authentication_and_Impersonation"&gt;&lt;/a&gt;Authentication and Impersonation&lt;/h2&gt;
&lt;p&gt;As mentioned previously the Client Credentials REST Host uses Oauth to authenticate, specifically a client credentials grant. &amp;nbsp; This means that when it is using the default user it requests an access token for that user and it is used to authenticate all non-impersonated REST requests. &amp;nbsp;If you are impersonating it will obtain a token for that user and use it for the scope of the impersonation. &amp;nbsp;To avoid multiple token requests the host will cache the users it gets tokens for. Because a user can be cached and the token may have expired, the host will also handle getting refresh tokens as well.&lt;/p&gt;
&lt;h3&gt;&lt;a id="Impersonation" name="Impersonation"&gt;&lt;/a&gt;Impersonation&lt;/h3&gt;
&lt;p&gt;Impersonation in this host requires 2 steps. &amp;nbsp;First you must use a host specific method called&amp;nbsp;&lt;em&gt;Impersonate&lt;/em&gt; which is an action that will allow requests made within the scope of that action to be run as another user.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt; var host = new ClientCredentialsRestHost(); //Assumes config values arein the config file
 host.Impersonate(&amp;quot;userA&amp;quot;, (h) =&amp;gt;
 {
    //Stuff in here runs as userA
 });&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Additionally all requests must set the enableImpersonation argument of the call to true(default). &amp;nbsp; Setting it to false would be a way to opt-out of the impersonation while still in the scope of the action.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="csharp"&gt;var host = new ClientCredentialsRestHost(); //Assumes config values arein the config file
host.Impersonate(&amp;quot;userA&amp;quot;, (h) =&amp;gt;
{
    //This will run as userA
    dynamic impersonatedResponse = host.GetToDynamic(2, &amp;quot;info.json&amp;quot;,true);
    //This will run as the default user
    dynamic notImpersonatedResponse = host.GetToDynamic(2, &amp;quot;info.json&amp;quot;, false);
});&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;
</description></item></channel></rss>