Skip to content

Instantly share code, notes, and snippets.

@sitefinitySDK
Last active November 30, 2022 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sitefinitySDK/44beadd78ca16e751690e9b648508fd1 to your computer and use it in GitHub Desktop.
Save sitefinitySDK/44beadd78ca16e751690e9b648508fd1 to your computer and use it in GitHub Desktop.
SF_10.1, SF_10.2, SF_11.0, SF_11.1, SF_11.2, SF_12.0, SF_12.1, SF_12.2, SF_13.0, SF_13.1, SF_13.2, SF_13.3, SF_14.0, SF_14.1, SF_14.2, SF_14.3 - https://docs.sitefinity.com/request-access-token-for-calling-web-services
using IdentityModel.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
namespace Sample
{
public class Program
{
private static TokenClient tokenClient;
public static void Main(string[] args)
{
// The token client is used to make calls to the STS endpoint where you can retrieve the access token.
// The AuthenticationStyle.PostValues tells that the parameters will be send into the body of the request
// For the different Authentication Styles please check https://github.com/IdentityModel/IdentityModel
tokenClient = new TokenClient(TokenEndpoint, ClientId, ClientSecret, AuthenticationStyle.PostValues);
TokenResponse tokenResponse = RequestToken();
string accessToken = tokenResponse.AccessToken;
//The purpose of the refresh token is to retrieve new access token when the ols expires
string refreshToken = tokenResponse.RefreshToken;
Console.WriteLine("Access token: {0}", accessToken);
Console.WriteLine("Refresh token: {0}", refreshToken);
string reponseHtml = CallApi(accessToken);
Console.WriteLine("Api Response: {0}", reponseHtml);
var newTokenResponse = RefreshToken(refreshToken);
Console.WriteLine("New access token: {0}", accessToken);
Console.WriteLine("New refresh token: {0}", refreshToken);
}
public static TokenResponse RequestToken()
{
//This is call to the token endpoint with the parameters that are set
TokenResponse tokenResponse = tokenClient.RequestResourceOwnerPasswordAsync(Username, Password, Scopes, AdditionalParameters).Result;
if (tokenResponse.IsError)
{
throw new ApplicationException("Couldn't get access token. Error: " + tokenResponse.Error);
}
return tokenResponse;
}
public static string CallApi(string accessToken)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(WebApiNewsEndPoint);
request.ContentType = "application/json";
request.Method = "GET";
request.Headers.Add("Authorization", "Bearer " + accessToken);
string html = string.Empty;
WebResponse response = request.GetResponse();
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
return html;
}
public static TokenResponse RefreshToken(string refreshToken)
{
//This is call to the token endpoint that can retrieve new access and refresh token from the current refresh token
return tokenClient.RequestRefreshTokenAsync(refreshToken).Result;
}
public const string ClientId = "testApp";
public const string ClientSecret = "secret";
public const string TokenEndpoint = "http://yousitefinitysite/Sitefinity/Authenticate/OpenID/connect/token";
public const string Username = "test@test.test";
public const string Password = "password";
public const string Scopes = "openid offline_access";
public static readonly Dictionary<string, string> AdditionalParameters = new Dictionary<string, string>()
{
{ "membershipProvider", "Default" }
};
public const string WebApiNewsEndPoint = "http://yoursitefinitysite/api/default/newsitems";
}
}
@mitrov
Copy link

mitrov commented May 16, 2018

Hi

I tried this code and I was able to access the newsitems api. However I have trouble calling a
webservice like "/Services/Security/Roles.svc" with a bearer token. I get a 401 Unauthorized response.
Could you provide an example?

Best regards
Džoka

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment