使用 ADAL.net 验证 Azure AD 凭据的自定义 Web 应用登录页面
Custom web app login page using ADAL.net to validate Azure AD credentials
在 Web 应用程序自定义登录页面中验证 Active Directory 用户名和密码。我知道在我自己的自定义登录页面中可能会降低应用程序安全性。我可以知道如何通过在应用程序自定义登录页面中验证用户名和密码来获取访问令牌。
看起来像下面这样,但我想要更详细的信息...使用 ADAL.net 中的用户名和密码向 Azure AD 进行身份验证:
https://github.com/Azure-Samples/active-directory-dotnet-native-headless
像这样:
var authContext = new AuthenticationContext(Authority);
var userCredential = new UserPasswordCredential(username, password);
var token = authContext.AcquireToken(ResourceUrl, ClientId, userCredential);
您需要使用资源所有者密码凭据授予。这里有更多信息:https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth-ropc。请记住,它会降低您的安全性。
下面的link更有用:
使用 ADAL.net 中的用户名和密码向 Azure AD 进行身份验证:https://github.com/Azure-Samples/active-directory-dotnet-native-headless
这个更详细,但缺点是,有过时的截图:
https://vincentlauzon.com/2017/01/29/authenticating-to-azure-ad-non-interactively/
这是新的 UI 信息:
https://docs.microsoft.com/en-us/azure/active-directory/develop/app-registrations-training-guide
简而言之:根据第一个 link 在服务器上创建的两个 Azure 应用程序:一个作为客户端,另一个作为服务(资源)。上面 link 唯一没有提到的是将客户端应用程序(在 AD Azure 设置中)"default client type" 设置为是(在身份验证下)。以下代码替换了 LDAP。
在ApplicationUserManager中调用以下方法。
public override async Task<bool> CheckPasswordAsync(ApplicationUser user, string password)
{
return await Task.Run(() => IsAuthenticated(user.UserName, password));
}
private bool IsAuthenticated(string username, string pwd)
{
bool authenticated = false;
try
{
//token cache not to set, that is the reason sending the null value
var authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/{tenant-ID}/oauth2/token", true, null);
AuthenticationResult authenticationResult = null;
var credential = new UserPasswordCredential(username, pwd);
authenticationResult = authenticationContext.AcquireTokenAsync("https://tenantname.onmicrosoft.com/ServiceAppId", "ClientAppId", credential).Result;
authenticated = true;
}
catch (Exception ex)
{
return false;
//not authenticated due to some other exception
}
return authenticated;
}
希望对您有所帮助。
在 Web 应用程序自定义登录页面中验证 Active Directory 用户名和密码。我知道在我自己的自定义登录页面中可能会降低应用程序安全性。我可以知道如何通过在应用程序自定义登录页面中验证用户名和密码来获取访问令牌。
看起来像下面这样,但我想要更详细的信息...使用 ADAL.net 中的用户名和密码向 Azure AD 进行身份验证: https://github.com/Azure-Samples/active-directory-dotnet-native-headless
像这样:
var authContext = new AuthenticationContext(Authority);
var userCredential = new UserPasswordCredential(username, password);
var token = authContext.AcquireToken(ResourceUrl, ClientId, userCredential);
您需要使用资源所有者密码凭据授予。这里有更多信息:https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth-ropc。请记住,它会降低您的安全性。
下面的link更有用:
使用 ADAL.net 中的用户名和密码向 Azure AD 进行身份验证:https://github.com/Azure-Samples/active-directory-dotnet-native-headless
这个更详细,但缺点是,有过时的截图: https://vincentlauzon.com/2017/01/29/authenticating-to-azure-ad-non-interactively/
这是新的 UI 信息: https://docs.microsoft.com/en-us/azure/active-directory/develop/app-registrations-training-guide
简而言之:根据第一个 link 在服务器上创建的两个 Azure 应用程序:一个作为客户端,另一个作为服务(资源)。上面 link 唯一没有提到的是将客户端应用程序(在 AD Azure 设置中)"default client type" 设置为是(在身份验证下)。以下代码替换了 LDAP。
在ApplicationUserManager中调用以下方法。
public override async Task<bool> CheckPasswordAsync(ApplicationUser user, string password)
{
return await Task.Run(() => IsAuthenticated(user.UserName, password));
}
private bool IsAuthenticated(string username, string pwd)
{
bool authenticated = false;
try
{
//token cache not to set, that is the reason sending the null value
var authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/{tenant-ID}/oauth2/token", true, null);
AuthenticationResult authenticationResult = null;
var credential = new UserPasswordCredential(username, pwd);
authenticationResult = authenticationContext.AcquireTokenAsync("https://tenantname.onmicrosoft.com/ServiceAppId", "ClientAppId", credential).Result;
authenticated = true;
}
catch (Exception ex)
{
return false;
//not authenticated due to some other exception
}
return authenticated;
}
希望对您有所帮助。