Created
January 26, 2017 13:07
-
-
Save milannankov/e4eb46a4c293fb42b265e4d3404e51a9 to your computer and use it in GitHub Desktop.
Sitefinity Web API Authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async void AuthenticateAndTest(object sender, RoutedEventArgs e) | |
{ | |
var token = await this.GetAuthorizationToken(); | |
var jsonResponse = await this.CallSitefinityService(token); | |
} | |
private async Task<string> GetAuthorizationToken() | |
{ | |
var requestBody = new FormUrlEncodedContent(new[] { | |
new KeyValuePair<string, string>("wrap_name", "test"), | |
new KeyValuePair<string, string>("wrap_password", "12345678") | |
}); | |
var client = new HttpClient(); | |
client.BaseAddress = new Uri("http://localhost:50000"); | |
var loginResult = await client.PostAsync("/Sitefinity/Authenticate/SWT", requestBody); | |
var responseText = await loginResult.Content.ReadAsStringAsync(); | |
var token = this.GetAuthenticationToken(responseText); | |
return token; | |
} | |
private string GetAuthenticationToken(string authenticationResponse) | |
{ | |
var responseParameters = System.Web.HttpUtility.ParseQueryString(authenticationResponse); | |
var token = responseParameters["wrap_access_token"]; | |
return token; | |
} | |
private async Task<string> CallSitefinityService(string token) | |
{ | |
var httpClient = new HttpClient(); | |
httpClient.BaseAddress = new Uri("http://localhost:50000/"); | |
var request = new HttpRequestMessage(HttpMethod.Get, "Sitefinity/Services/Content/ImageService.svc"); | |
request.Headers.Add("Authorization", $"WRAP access_token=\"{token}\""); | |
var serviceResult = await httpClient.SendAsync(request); | |
var jsonResult = await serviceResult.Content.ReadAsStringAsync(); | |
return jsonResult; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment