Azure SQL支持通过AAD客户端ID和Secret访问吗?如果是,将通过什么TSQL授予对AAD ClientId的访问权限,我可以使用SSMS通过AAD客户端和密码连接到Azure SQL吗?
答案 0 :(得分:0)
是的,您可以使用访问令牌(AD令牌)
应用程序/服务可以从Azure检索访问令牌 Active Directory并使用它来连接到SQL Azure数据库。
在注册APP时在signonURL中提供任何内容(http://mytokentest
)
CREATE USER [mytokentest] FROM EXTERNAL PROVIDER
在客户端应用中尝试以下代码
public static void main(String[] args) throws Exception {
// Retrieve the access token from the AD.
String spn = "https://database.windows.net/";
String stsurl = "https://login.microsoftonline.com/..."; // Replace with your STS URL.
String clientId = "1846943b-ad04-4808-aa13-4702d908b5c1"; // Replace with your client ID.
String clientSecret = "..."; // Replace with your client secret.
AuthenticationContext context = new AuthenticationContext(stsurl, false, Executors.newFixedThreadPool(1));
ClientCredential cred = new ClientCredential(clientId, clientSecret);
Future<AuthenticationResult> future = context.acquireToken(spn, cred, null);
String accessToken = future.get().getAccessToken();
System.out.println("Access Token: " + accessToken);
// Connect with the access token.
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName("aad-managed-demo.database.windows.net"); // Replace with your server name.
ds.setDatabaseName("demo"); // Replace with your database name.
ds.setAccessToken(accessToken);
ds.setHostNameInCertificate("*.database.windows.net");
try (Connection connection = ds.getConnection();
Statement stmt = connection.createStatement();) {
ResultSet rs = stmt.executeQuery("SELECT SUSER_SNAME()");
if (rs.next()) {
System.out.println("You have successfully logged on as: " + rs.getString(1));
}
}
}
Follow here with Sample Java Code