ASP.NET 核心 blazor webassembly 获取用于 Identity Server 4 Postman 测试的令牌

ASP.NET core blazor webassembly getting token for Identity Server 4 Postman testing

我正在尝试在 blazor webassembly asp.net 核心托管应用程序中使用邮递员测试我的 api 以及身份服务器 4 个个人帐户。不幸的是,尽管尝试了许多不同的配置选项来获取新令牌,但我一直无法获得一个。这是我试过的

这会导致 postman 浏览器模拟器弹出并且永远不会完成。

这个失败了,但我得到了 info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2] Authorization failed. These requirements were not met: DenyAnonymousAuthorizationRequirement: Requires an authenticated user.

的更多信息错误

但是,当我尝试使用默认的测试用户名和密码时,我得到 Error: unauthorized_client

我按照 this article 中的设置一步一步地使用 API authorization options 而不是配置文件服务选项(并且我在本地开发,而不是使用 azure。)我需要什么做什么来获得令牌?感谢您的帮助,谢谢。

编辑:尝试在 ConfigureServices 中添加新客户端,但同样的行为发生在 postman 浏览器模拟器弹出并且从未完成。

services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options => {
                    options.IdentityResources["openid"].UserClaims.Add("name");
                    options.ApiResources.Single().UserClaims.Add("name");
                    options.IdentityResources["openid"].UserClaims.Add("role");
                    options.ApiResources.Single().UserClaims.Add("role");
                    options.Clients.Add(new IdentityServer4.Models.Client()
                    {
                        ClientId = "postman",

                        AllowedGrantTypes = GrantTypes.Code,
                        AllowOfflineAccess = true,
                        ClientSecrets = { new Secret("secret".Sha256()) },

                        RedirectUris = { "http://localhost:21402/signin-oidc", "https://oauth.pstmn.io/v1/browser-callback" },
                        PostLogoutRedirectUris = { "http://localhost:21402/" },
                        FrontChannelLogoutUri = "http://localhost:21402/signout-oidc",

                        AllowedScopes =
                        {
                            IdentityServerConstants.StandardScopes.OpenId,
                            IdentityServerConstants.StandardScopes.Profile,
                            IdentityServerConstants.StandardScopes.Email,

                            "Onero.ServerAPI"
                        },
                    });
                });

如果您使用的是授权代码授予,请将此 URL 用于回调 URL 并保留 使用浏览器授权 未选中。您还需要将 URL 添加到您应用的 RedirectUris 列表中。

https://oauth.pstmn.io/v1/browser-callback

此页面只是向授权弹出窗口的父级(即 Postman window)发送一条消息

<script>
    let data = {
        isAuthCallback: true,
        queryString: window.location.search || '',
        hash: window.location.hash || ''
    };

    window.opener.postMessage(data, '*');
</script>

如果您不想允许这个 URL(您可能想保护令牌免受第 3 方的攻击),您可以在您的应用中托管此页面。

[HttpGet("postman-callback")]
public IActionResult PostmanCallback()
{
    return new ContentResult {
        ContentType = "text/html",
        StatusCode = 200,
        Content = @"
<html><body><script>
let data = {
    isAuthCallback: true,
    queryString: window.location.search || '',
    hash: window.location.hash || ''
};

window.opener.postMessage(data, '*');
</script></body></html>"
    };
}

经过几天阅读文档和博客以了解整体情况后,我终于能够做到了!我所做的是以下内容:

仔细查看启动我的服务器项目的输出,这是我看到的:

这让我意识到我在 Postman 中为 Auth URL 使用了错误的端点。所以我把它改成了https://localhost:5001/connect/authorize。然后我在 Postman

中使用了这个配置

在服务器的 Startup.cs 文件中像这样添加 Postman 客户端

 services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options => {
                    ...
                    options.Clients.Add(new IdentityServer4.Models.Client()
                    {
                        ClientId = "Postman",

                        AllowedGrantTypes = GrantTypes.Code,
                        AllowOfflineAccess = true,
                        ClientSecrets = { new Secret("secret".Sha256()) },

                        RedirectUris = { "http://localhost:21402/signin-oidc", "https://oauth.pstmn.io/v1/browser-callback" },
                        PostLogoutRedirectUris = { "http://localhost:21402/" },
                        FrontChannelLogoutUri = "http://localhost:21402/signout-oidc",
                        AllowedScopes =
                        {
                            "Onero.ServerAPI"
                        },
                    });;
                });

最后弹出那个小 Postman 页面,将我带到默认的 IdentityServer AuthUI 页面,使用我的默认用户登录然后我们开始,最后得到该死的令牌。

最大收获:确保阅读服务器输出以确保您的端点正确,以便您可以正确填写 Postman 中的参数。

感谢您的帮助!