不是从ASP .NET Webforms中的Startup.cs文件创建会话?

问题描述 投票:0回答:1

我的ASP .NET Webforms(Web应用程序)正在使用Microsoft Graph API进行登录,并且我试图存储来自AcquireTokenByAuthorizationCode方法的访问令牌,以用于下一个对Graph API的请求。

但是这失败了,因为未创建会话并提供了空值。

以上代码正在startup.cs文件中运行。

    private async Task OnAuthorizationCodeReceivedAsync(AuthorizationCodeReceivedNotification notification)
    {
        var idClient = ConfidentialClientApplicationBuilder.Create(clientId)
            .WithRedirectUri(postLogoutRedirectUri)
            .WithClientSecret(appKey)
            .Build();
        var signedInUser = new ClaimsPrincipal(notification.AuthenticationTicket.Identity);

        var tokenStore = new SessionTokenStore(idClient.UserTokenCache, HttpContext.Current, signedInUser);
        HttpContext.Current.Response.Cookies.Add(new HttpCookie("name","test"));
        string message;
        string debug;

        try
        {
            string[] AppScopes = scopes.Split(' ');

            var result = await idClient.AcquireTokenByAuthorizationCode(
                AppScopes, notification.Code).ExecuteAsync();

            message = "Access token retrieved.";
            debug = result.AccessToken;

        }
        catch (MsalException ex)
        {
            message = "AcquireTokenByAuthorizationCodeAsync threw an exception";
            debug = ex.Message;
        }

    }
microsoft-graph
1个回答
0
投票

这里有同样的问题,即使更改管道标记,也无法使其在Startup上运行。您设法找到解决方案了吗?

© www.soinside.com 2019 - 2024. All rights reserved.