Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSAL V1.x how to update client id and authority dynamically (i am using angular 7) #7476

Closed
2 tasks
ganeshgudaghe opened this issue Dec 19, 2024 · 1 comment
Closed
2 tasks
Labels
bug-unconfirmed A reported bug that needs to be investigated and confirmed msal-angular Related to @azure/msal-angular package msal-browser Related to msal-browser package public-client Issues regarding PublicClientApplications question Customer is asking for a clarification, use case or information.

Comments

@ganeshgudaghe
Copy link

ganeshgudaghe commented Dec 19, 2024

Core Library

MSAL.js (@azure/msal-browser)

Core Library Version

1.x

Wrapper Library

MSAL Angular (@azure/msal-angular)

Wrapper Library Version

1.x

Public or Confidential Client?

Public

Description

I am using the @azure/msal-angular version ^1.1.2, and I am facing an issue on how to update the MsalModule dynamically
i am refering the code base https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/msal-angular-v1/samples/msal-angular-samples/angular7-sample-app/src/app/app.module.ts

@NgModule({
    declarations: [],
   imports: [  MsalModule.forRoot({ auth: {
            clientId: '',
            authority: '',
            redirectUri: '',
            postLogoutRedirectUri: '',
            navigateToLoginRequestUrl: true
        } },
            {
                popUp: !isIE,
                consentScopes: [
                    'profile',
                    'user.read',
                    'offline_access'
                ],
                unprotectedResources: ['https://www.microsoft.com'],
                protectedResourceMap,
                extraQueryParameters: {},
            })
            ]

Because MSAL 1.x does not support the .instance method (which is available in version 2.x), I am able to update the configuration dynamically in version 2.x, but not in 1.x. Could you please guide me on how to update the configuration dynamically in version 1.x?

Error Message

No response

MSAL Logs

No response

Network Trace (Preferrably Fiddler)

  • Sent
  • Pending

MSAL Configuration

@NgModule({
    declarations: [],
   imports: [  MsalModule.forRoot({ auth: {
            clientId: '',
            authority: '',
            redirectUri: '',
            postLogoutRedirectUri: '',
            navigateToLoginRequestUrl: true
        } },
            {
                popUp: !isIE,
                consentScopes: [
                    'profile',
                    'user.read',
                    'offline_access'
                ],
                unprotectedResources: ['https://www.microsoft.com'],
                protectedResourceMap,
                extraQueryParameters: {},
            })
            ]

Relevant Code Snippets

@NgModule({
    declarations: [],
   imports: [  MsalModule.forRoot({ auth: {
            clientId: '',
            authority: '',
            redirectUri: '',
            postLogoutRedirectUri: '',
            navigateToLoginRequestUrl: true
        } },
            {
                popUp: !isIE,
                consentScopes: [
                    'profile',
                    'user.read',
                    'offline_access'
                ],
                unprotectedResources: ['https://www.microsoft.com'],
                protectedResourceMap,
                extraQueryParameters: {},
            })
            ]

Reproduction Steps

how to update configuration dynamically

Expected Behavior

how to update configuration dynamically

Identity Provider

Entra ID (formerly Azure AD) / MSA

Browsers Affected (Select all that apply)

Chrome, Firefox, Edge, Safari

Regression

azure/msal-angular": "1.1.2

@ganeshgudaghe ganeshgudaghe added bug-unconfirmed A reported bug that needs to be investigated and confirmed question Customer is asking for a clarification, use case or information. labels Dec 19, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs: Attention 👋 Awaiting response from the MSAL.js team label Dec 19, 2024
@github-actions github-actions bot added msal-angular Related to @azure/msal-angular package msal-browser Related to msal-browser package public-client Issues regarding PublicClientApplications labels Dec 19, 2024
@ganeshgudaghe
Copy link
Author

After a lot of trial and error, I finally found the solution to update the MSAL 1.x configuration dynamically. Here is the code:


import {MsalService } from '@azure/msal-angular';
import * as Msal from 'msal';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  private msalApp: Msal.UserAgentApplication;
  private defaultConfig: Msal.Configuration = {
    auth: {
      clientId: 'initial-client-id',  // Default clientId
      authority: 'https://login.microsoftonline.com/your-tenant-id',  // Default authority
    },
    cache: {
      cacheLocation: 'localStorage',
      storeAuthStateInCookie: false,
    },
  };

  constructor(private authService: MsalService) { }
      ngOnInit() {
      }


  loginAzure() {
    this.msalApp = null;
    const updatedConfig: Msal.Configuration = {
      auth: { // update dynamic data here
        clientId: config.clientId, // dynamic clientId
        authority: config.authority, // dynamic authority
        redirectUri: config.redirectUrl, // dynamic redirectUrl
        postLogoutRedirectUri: config.redirectUrl, // dynamic redirectUrl
        navigateToLoginRequestUrl: true
      },
      cache: this.defaultConfig.cache,
    };
    this.msalApp = new Msal.UserAgentApplication(updatedConfig);
    this.msalApp.loginPopup().then(response => {
      console.log('response', response)
    }).catch(error => {
      console.error(error);
    });
  }

}

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Needs: Attention 👋 Awaiting response from the MSAL.js team label Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-unconfirmed A reported bug that needs to be investigated and confirmed msal-angular Related to @azure/msal-angular package msal-browser Related to msal-browser package public-client Issues regarding PublicClientApplications question Customer is asking for a clarification, use case or information.
Projects
None yet
Development

No branches or pull requests

1 participant