我需要使用Azure Active Directory中的用户凭据(用户名和密码)登录MVC Web应用程序。知道怎么做吗?
答案 0 :(得分:0)
您可以使用以下方法登录到Azure活动目录。为此,您需要直接将您的帐户配置为活动帐户,并直接在您的方法中提供其值,这将返回一个令牌,并且您必须从该令牌中初始化您的类,例如Microsoft.Azure.TokenCloudCredentials(“ xxxxxxxx-xxxx- xxxx-xxx-xxxxxxxxx“,accessToken),然后您就可以执行任何操作。
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;
}