1
votes

I have a requirement to login to MVC web application using user credentials(username and password) in Azure Active Directory. Any idea how to do ?

1

1 Answers

0
votes

You can login into the azure active directory by using the following method. For this you need to configure your account in active directly and give its values directly in your method and this will return you a token and from that token you have to initialize your class like this here Microsoft.Azure.TokenCloudCredentials("xxxxxxxx-xxxx-xxxx-xxx-xxxxxxxxx", accessToken) and then you can perform any operation.

 public AuthenticationResult GetAccessToken()
{
    string hardcodedUsername = "activedirectoryusername";
    string hardcodedPassword = "activedirectorypassword";

    string tenant = "abc.onmicrosoft.com";
    string clientId = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
    string resourceHostUri = "https://management.core.windows.net/";
    string aadInstance = "https://login.windows.net/{0}";
    //string aadInstance = "https://login.windows.net/{0}/oauth2/authorize";

    AuthenticationContext authenticationContext = new AuthenticationContext(String.Format(aadInstance, tenant));
    UserCredential userCredential = new UserPasswordCredential(hardcodedUsername, hardcodedPassword);
    AuthenticationResult authenticationResult = null;
    authenticationResult = authenticationContext.AcquireTokenAsync(resourceHostUri, clientId, userCredential).Result;

    return authenticationResult;
}