Kurkula
Kurkula

Reputation: 6762

authContext.AcquireTokenByAuthorizationCode not working with latest System.IdentityModel.Clients.ActiveDirectory

I used the below code for azure active directory authentication. it fails at authContext.AcquireTokenByAuthorizationCode stating that this method does not exist. when I tried to verify it shows that in version 3.1 dll. There is authContext.AcquireTokenByAuthorizationCodeAsync method. I am unable to modify this code to make it async as it is on startup. Any suggestions on this to convert to async

 public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
           ConfigureAuth(app);
        }
    }


public void ConfigureAuth(IAppBuilder app)
        {
            ApplicationDbContext db = new ApplicationDbContext();

            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            app.UseCookieAuthentication(new CookieAuthenticationOptions());

            app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    ClientId = clientId,
                    Authority = Authority,
                    PostLogoutRedirectUri = postLogoutRedirectUri,

                    Notifications = new OpenIdConnectAuthenticationNotifications()
                    {
                        //If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
                        AuthorizationCodeReceived = (context) =>
                        {
                            var code = context.Code;
                            ClientCredential credential = new ClientCredential(clientId, appKey);
                            string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
                            AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID));
                            AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
                            code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);

                            return Task.FromResult(0);
                        }
                    }
                });
        }

Upvotes: 2

Views: 4585

Answers (1)

Shawn Tabrizi
Shawn Tabrizi

Reputation: 12434

There were a number of breaking changes from ADAL v2 to ADAL v3. Probably your quickest solution here would be to simply use the older version of ADAL, which you can download from Nuget here.

Otherwise, you can look at our existing samples which have migrated to ADAL v3 here.

Upvotes: 3

Related Questions