简体   繁体   English

支持Azure AD中的用户证书身份验证

[英]Support for user certificate authentification in Azure AD

I'am searching for public/private key-based authentication for users with Azure-ActiveDirectory but can't find any hints in Azure AD Authentification Scenarios . 我正在为具有Azure-ActiveDirectory的用户搜索基于公钥/私钥的身份验证,但在Azure AD身份验证方案中找不到任何提示。 Every user should bring his own key pair. 每个用户都应携带自己的密钥对。 Any suggestions how to achieve it? 有什么建议如何实现吗?

Azure AD supports OAuth 2.0 to authorize the third-party apps and the OAuth 2.0 support to acquire the token using the app's client credential. Azure AD支持OAuth 2.0来授权第三方应用程序,并且OAuth 2.0支持使用应用程序的客户端凭据来获取令牌。

There are two ways to acquire the token for the client credential flow. 有两种方法可以获取客户端凭证流的令牌。 First is that using the keys which generated by the Azure portal like figure below: 首先是使用由Azure门户生成的密钥,如下图所示: 在此处输入图片说明

And here is a figure about token request using the key(client_secret): 这是有关使用密钥(client_secret)的令牌请求的图: 在此处输入图片说明

Another way is using the cert. 另一种方法是使用证书。 Technical speaking, the cert is a pair of public/private key. 从技术上讲,证书是一对公钥/私钥。 We will store the information of public key with the app. 我们将使用该应用程序存储公钥信息。 When we require to prove we are the owner of the third-party apps, we need to sign the message with the private key and Azure AD with verify the messages with public key. 当我们需要证明我们是第三方应用程序的所有者时,我们需要使用私钥和Azure AD对消息签名,并使用公钥验证消息。

To store the cert information with apps, we need to change the manifest of app. 要将证书信息与应用程序存储在一起,我们需要更改应用程序的清单。 Here is the detail steps to use a self-signed certificate from here : 这是从此处使用自签名证书的详细步骤:

1.Generate a self-signed certificate: 1.生成自签名证书:

makecert -r -pe -n "CN=MyCompanyName MyAppName Cert" -b 03/15/2015 -e 03/15/2017 -ss my -len 2048

2.Open the Certificates MMC snap-in and connect to your user account. 2.打开“证书” MMC管理单元并连接到您的用户帐户。

3.Find the new certificate in the Personal folder and export the public key to a base64-encoded file (for example, mycompanyname.cer). 3.在“个人”文件夹中找到新证书,并将公共密钥导出到以base64编码的文件(例如,mycompanyname.cer)。 Your application will use this certificate to communicate with AAD, so make sure you retain access to the private key as well. 您的应用程序将使用此证书与AAD进行通信,因此请确保您也保留对私钥的访问。

Note You can use Windows PowerShell to extract the thumbprint and base64-encoded public key. 注意可以使用Windows PowerShell提取指纹和base64编码的公钥。 Other platforms provide similar tools to retrieve properties of certificates. 其他平台提供了类似的工具来检索证书的属性。

4.From the Windows PowerShell prompt, type and run the following: 4.在Windows PowerShell提示符下,键入并运行以下命令:

$cer = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 

$cer.Import("mycer.cer") 
$bin = $cer.GetRawCertData() 
$base64Value = [System.Convert]::ToBase64String($bin) 
$bin = $cer.GetCertHash() 
$base64Thumbprint = [System.Convert]::ToBase64String($bin) 
$keyid = [System.Guid]::NewGuid().ToString()

5.Store the values for $base64Thumbprint, $base64Value and $keyid, to be used when you update your application manifest in the next set of steps. 5.存储在下一组步骤中更新应用程序清单时要使用的$ base64Thumbprint,$ base64Value和$ keyid的值。 Using the values extracted from the certificate and the generated key ID, you must now update your application manifest in Azure AD. 现在,必须使用从证书中提取的值和生成的密钥ID,来更新Azure AD中的应用程序清单。

6.In the Azure Management Portal, select your application and choose Configure in the top menu. 6.在Azure管理门户中,选择您的应用程序,然后在顶部菜单中选择“配置”。

7.In the command bar, click Manage manifest and select Download Manifest. 7.在命令栏中,单击“管理清单”,然后选择“下载清单”。

在此处输入图片说明

8.Open the downloaded manifest for editing and replace the empty KeyCredentials property with the following JSON: 8.打开下载的清单以进行编辑,并将空的KeyCredentials属性替换为以下JSON:

"keyCredentials": [
  {
    "customKeyIdentifier": "$base64Thumbprint_from_above",
    "keyId": "$keyid_from_above",
    "type": "AsymmetricX509Cert",
    "usage": "Verify",
    "value": "$base64Value_from_above"
  }
],

9.Save your changes and upload the updated manifest by clicking Manage manifest in the command bar, selectingUpload manifest, browsing to your updated manifest file, and then selecting it. 9.通过单击命令栏中的“管理清单”,选择“上传清单”,浏览到更新的清单文件,然后选择它,保存更改并上传更新的清单。

And below is the figure about token request using the cert: 下面是有关使用证书的令牌请求的图: 在此处输入图片说明

In the article above, it generate the client_assertion from scratch. 在上面的文章中,它从头开始生成client_assertion。 We can also use the ADAL library to help us and authenticate for a daemon apps: 我们还可以使用ADAL库来帮助我们并对守护程序进行身份验证:

string authority = $"https://login.microsoftonline.com/{tenant}";

var thumbprint="";
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByThumbprint, thumbprint, false);
X509Certificate2 cert = fcollection[0];

var certCred = new ClientAssertionCertificate(clientId, cert);
var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority);
AuthenticationResult result = null;
try
{
    result = await authContext.AcquireTokenAsync(resource, certCred);
}
catch (Exception ex)
{
}

return result.AccessToken;

And if you want to use the cert to authorize for the OAuth2.0 code grant flow, you also can refer the code sample here . 而且,如果您想使用证书授权OAuth2.0代码授予流程,还可以在此处参考代码示例。

If you're looking for a programmatic method to authenticate users (not apps) in Azure AD using certificates, this is not currently possible. 如果您正在寻找一种编程方法来使用证书对Azure AD中的用户(而非应用程序)进行身份验证,则目前无法实现。

It is possible to do certificate-based authentication in AD FS ( eg ), and it is possible to federate authentication for users from Azure AD to AD FS . 可以做到的AD FS(基于证书的验证例如 ),并且可以以联合身份验证的用户从Azure的AD到AD FS However, this requires the overhead of Windows Server AD, Azure AD Connect and AD FS, and I don't think the federated authentication can be achieved in a programmatic way (ie without prompting the user to choose a certificate). 但是,这需要Windows Server AD,Azure AD Connect和AD FS的开销,并且我认为不能以编程方式(即,不提示用户选择证书)来实现联合身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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