I am trying to get key vault secrets in my Azure account. I am using Msal.js for that. I can successfully log in to my app with azure but while I am trying to get secret. Here is my function to get secret:
public getSecret(secretName: string): any {
const keyVaultUrl = 'https://denemekeyvault.vault.azure.net/';
const apiUrl = `${keyVaultUrl}secrets/${secretName}?api-version=7.4`;
console.log(apiUrl);
this.getAccessToken().then(token => {
const headers = new HttpHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
});
console.log("headers", headers.getAll('Authorization'));
this.http.get(apiUrl, { headers: headers }).subscribe(
response => {
console.log(response);
},
error => {
console.error('token:'+token+'headers:'+headers+'API çağrısı sırasında hata oluştu:', error);
}
);
});
}
I tried on Postman to do these actions and i succeed but in Angular I took CORS error.
I expect to get key vault secret but i did not happened beacuse i got CORS error.
I successfully retrieved the Azure Key Vault Secret using an access token with the backend code in JavaScript and frontend in Angular.
backend :
server.js :
frontend :
keyvault.service.ts :
app.component.ts :
app.module.ts :
app.component.html :
I granted access to my Azure AD app (which utilized an access token) to retrieve the secret from the Azure Key Vault, as demonstrated below:
Output :
The backend code ran successfully, as shown below.
I retrieved the Azure Key Vault secret in the browser, as shown below.
Then, I started my frontend code, as shown below.
I successfully retrieved the Azure Key Vault secret in the browser, as shown below.