首页 > 解决方案 > How can I allow anonymous access to an otherwise protected resource through a unique URL in ASP.NET Core

问题描述

To access resources in my API the user needs to be authorized, which is handled by

[Authorize]

I need to find a way to generate a unique URL for a specific resource that can be used to access that resource without the user having to authenticate.

标签: c#asp.net-core

解决方案


您可以将该Authorize属性应用于该类,这将强制所有端点要求授权。

然后,通过AllowAnonymous在特定端点上指定属性,您将覆盖上述要求并允许用户在不登录的情况下访问 MyAnonymousEndpoint。

[Authorize]
public class MySecureClass
{
    [AllowAnonymous]
    public IActionResult MyAnonymousEndpoint()
    {

    }
}

推荐阅读