Search code examples
azure-ad-msal

Using MSAL Go Library to do OIDC authentication


I have been playing with the MSAL Go library a few days and I am still struck with how to use it to do OIDC authentication to Microsoft EntraID. I keep getting the error in my browser

The redirect URI 'http://localhost:51276' specified in the request does not match the redirect URIs configured for the application

The host is always localhost and the port seems to be random. I even have the explicit call to .withRedirectURI and set it to a totally random string, but it still falls to this pattern.

Below is my code:

func AcquireTokenOIDC() error {
client, err := public.New(
    clientID,
    public.WithAuthority(fmt.Sprintf("https://login.microsoftonline.com/%s/v2.0", tenantID)),
)
if err != nil {
    return fmt.Errorf("error creating public client: %s", err.Error())
}
scopes := []string{"openid", "profile", "email"}

result, err := client.AcquireTokenInteractive(context.Background(), scopes, public.WithRedirectURI("http://127.0.0.1"))

if err != nil {
    return fmt.Errorf("error acquiring access token: %s", err.Error())
}

accessToken = result.AccessToken
tokenExpiryTime = result.ExpiresOn
return nil

}

What am I missing?


Solution

  • I don't know why, but it appears Microsoft EntraID does not like http://127.0.0.1 as the redirect URI. I change it to http://localhost and the same code now works.