c# - authContext.AcquireTokenByAuthorizationCode 不适用于最新的 System.IdentityModel.Clients.ActiveDirectory

标签 c# azure async-await azure-active-directory

我使用以下代码进行 azure 事件目录身份验证。它在 authContext.AcquireTokenByAuthorizationCode 处失败,表明此方法不存在。当我尝试验证它显示在版本 3.1 dll 中。有 authContext.AcquireTokenByAuthorizationCodeAsync 方法。我无法修改此代码以使其在启动时异步。任何关于转换为异步的建议

 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);
                        }
                    }
                });
        }

最佳答案

从 ADAL v2 到 ADAL v3 存在许多重大更改。最快的解决方案可能是简单地使用旧版本的 ADAL,您可以从 Nuget here 下载该版本。 .

否则,您可以查看我们已迁移到 ADAL v3 的现有示例 here .

关于c# - authContext.AcquireTokenByAuthorizationCode 不适用于最新的 System.IdentityModel.Clients.ActiveDirectory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42607230/

相关文章:

c# - 快速轻松地从一个文件加载所需内容?

azure - 在 Azure ApplicationInsights 中创建 API key 所需的角色/权限

rust - FnOnce 的泛型,返回一个终生的 future

c# - HttpClient PostAsJsonAsync 方法无法正常工作(序列化无法正常工作)

c# - 如何处理 Startup.Configure 中的异步操作?

c# - 如何在运行时更改 SystemAccentColor (UWP)?

c# - 需要在 Winform 应用程序的 DataGrid View 中针对每一行放置一个 RadioButton

azure - 如何在Azure Web应用程序中设置节点版本?

azure - 应在 Azure Function 应用程序中的何处设置 ServicePointManager.DefaultConnectionLimit?

c# - 如何使用 CaSTLe Windsor - Fluent Interface 来注册通用接口(interface)?