Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Access CRM Online V9.1 Reporting Services from plugin code

Posted on by 20

Hello Experts,

I'm currently trying to access with ADAL authentication Token, but this error is happening

Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

string resource = "https://ServerURL.crm.dynamics.com/";

HttpResponseMessage response;
string clientId = "XXXXXXXXXXXXXX";
string clientSecret = "XXXXXXXXXXXXXX";
string authorityUri = "https://login.microsoftonline.com/"Domain"/oauth2/authorize"; // i tried this "login.microsoftonline.com/Tenantid" as guide, and I got same issue

AuthenticationContext authenticationContext = new AuthenticationContext(authorityUri, true);


var credential = new ClientCredential(clientId, clientSecret);
var result = await authenticationContext.AcquireTokenAsync(resource, credential);


var token =  result.AccessToken;

HttpClient request = new HttpClient();
request.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, completeUrl);
req.Method = HttpMethod.Post;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;


response = await request.SendAsync(req);
var responseText = await response.Content.ReadAsStringAsync();

var x = responseText.LastIndexOf("ReportSession=");
var y = responseText.LastIndexOf("ControlID=");

string[] ret = new string[2];
ret[0] = responseText.Substring(x + 14, 24);
ret[1] = responseText.Substring(y + 10, 32);

I followed this link to create the Authentication token: https://www.c-sharpcorner.com/article/generate-access-token-for-dynamics-365-single-tenant-server-to-server-authentica/

i hope some help, Kind Regards

Juan Camilo

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans