ASPC.Marvel.CrimeAPI.TokenHelper.GetAppOnlyAccessToken C# (CSharp) Метод

GetAppOnlyAccessToken() публичный статический Метод

Retrieves an app-only access token from ACS to call the specified principal at the specified targetHost. The targetHost must be registered for target principal. If specified realm is null, the "Realm" setting in web.config will be used instead.
public static GetAppOnlyAccessToken ( string targetPrincipalName, string targetHost, string targetRealm ) : OAuth2AccessTokenResponse
targetPrincipalName string Name of the target principal to retrieve an access token for
targetHost string Url authority of the target principal
targetRealm string Realm to use for the access token's nameid and audience
Результат OAuth2AccessTokenResponse
        public static OAuth2AccessTokenResponse GetAppOnlyAccessToken(
            string targetPrincipalName,
            string targetHost,
            string targetRealm)
        {
            if (targetRealm == null)
            {
                targetRealm = Realm;
            }

            string resource = GetFormattedPrincipal(targetPrincipalName, targetHost, targetRealm);
            string clientId = GetFormattedPrincipal(ClientId, HostedAppHostName, targetRealm);

            OAuth2AccessTokenRequest oauth2Request = OAuth2MessageFactory.CreateAccessTokenRequestWithClientCredentials(clientId, ClientSecret, resource);
            oauth2Request.Resource = resource;

            // Get token
            OAuth2S2SClient client = new OAuth2S2SClient();

            OAuth2AccessTokenResponse oauth2Response;
            try
            {
                oauth2Response =
                    client.Issue(AcsMetadataParser.GetStsUrl(targetRealm), oauth2Request) as OAuth2AccessTokenResponse;
            }
            catch (WebException wex)
            {
                using (StreamReader sr = new StreamReader(wex.Response.GetResponseStream()))
                {
                    string responseText = sr.ReadToEnd();
                    throw new WebException(wex.Message + " - " + responseText, wex);
                }
            }

            return oauth2Response;
        }