简体   繁体   中英

sample code for Microsoft Dynamics 365 CRM authentication and then redirect to C# console application

I want to C# console application code for Microsoft authentication, and when I click button redirect to Microsoft login page and after authentication redirect to C# application. Thanks in Advance

I'd suggest installing D365DeveloperExtensions for Visual Studio. This will give you a pre-built template for creating a console application that connects to a Dynamics 365 environment.

Alternatively below is sample code for login options with either a credential prompt (Microsoft login page) or without. Code taken fromthis blog .

//Azure Application Client ID
private const string _clientId = "00000000-0000-0000-0000-000000000000";
// Azure Application REPLY URL - can be anything here but it must be registered ahead of time
private const string _redirectUrl = "http://localhost/CrmWebApi";
//CRM URL
private const string _serviceUrl = "https://org.crm.dynamics.com";
//O365 credentials for authentication w/o login prompt
private const string _username = "administrator@org.onmicrosoft.com";
private const string _password = "password";
//Azure Directory OAUTH 2.0 AUTHORIZATION ENDPOINT
private const string _authority = 
 "https://login.microsoftonline.com/00000000-0000-0000-0000-000000000000";
private static AuthenticationResult _authResult;
AuthenticationContext authContext =
    new AuthenticationContext(_authority, false);
//Prompt for credentials
//_authResult = authContext.AcquireToken(
//    _serviceUrl, _clientId, new Uri(_redirectUrl));

//No prompt for credentials
UserCredential credentials = new UserCredential(_username, _password);
_authResult = authContext.AcquireToken(
    _serviceUrl, _clientId, credentials);

var token = _authResult.AccessToken;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM