根据客户端ID授权对API的调用

时间:2017-11-02 23:23:42

标签: c# identityserver3

我有一个C#API,它使用IdentityServer3来授权对方法的调用。

我想限制某个客户端可以访问哪些调用。客户端正在使用客户端凭据流,我正在尝试找到一种基于客户端ID授权呼叫的方法。

有没有办法开箱即用,还是需要编写自定义过滤器?

2 个答案:

答案 0 :(得分:1)

添加身份验证管理器如何 - 如果您查看Identity Server示例,例如" MVC身份验证"一,它在启动时注册一个自定义身份验证管理器,然后使用漂亮的简单资源属性来保护每个Controller操作。

e.g。这是你的创业公司

app.UseResourceAuthorization(new AuthorizationManager());

然后定义您的自定义Auth管理器类,如....

public class AuthorizationManager : ResourceAuthorizationManager
{
    public override Task<bool> CheckAccessAsync(ResourceAuthorizationContext context)
    {
        switch (context.Resource.First().Value)
        {
            case "PageAuthCheck":
                return AuthorizeContactDetails(context);
            default:
                return Nok();
        }
    }

    private Task<bool> AuthorizeContactDetails(ResourceAuthorizationContext context)
    {
        switch (context.Action.First().Value)
        {
            case "Read":
                return Eval(context.Principal.Identity.IsAuthenticated);
            case "Write":
                return Eval(context.Principal.HasClaim("role", "Admin"));
            default:
                return Nok();
        }
    }
}

之后,您可以像....一样保护您的控制器操作。

    [ResourceAuthorize("Read", "PageAuthCheck")]

显然,这只是一个使用用户声明角色检查的示例,但您应该可以根据客户端ID轻松更改此设置

答案 1 :(得分:0)

你能做的就是拥有一个clientid和一个clientSecret。您只需要实现一个逻辑,检查您的clientid和clientSecret是否有效访问您的应用程序。

检查此示例并根据您的需要进行更新

   public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {

        string clientId = string.Empty;
        string clientSecret = string.Empty;
        Client client = null;

        if (!context.TryGetBasicCredentials(out clientId, out clientSecret))
        {
            context.TryGetFormCredentials(out clientId, out clientSecret);
        }

        if (context.ClientId == null)
        {
            //Remove the comments from the below line context.SetError, and invalidate context 
            //if you want to force sending clientId/secrects once obtain access tokens. 
            context.Validated();
            //context.SetError("invalid_clientId", "ClientId should be sent.");
            return Task.FromResult<object>(null);
        }

        using (AuthRepository _repo = new AuthRepository())
        {
            client = _repo.FindClient(context.ClientId);
        }

        if (client == null)
        {
            context.SetError("invalid_clientId", string.Format("Client '{0}' is not registered in the system.", context.ClientId));
            return Task.FromResult<object>(null);
        }

        if (client.ApplicationType == Models.ApplicationTypes.NativeConfidential)
        {
            if (string.IsNullOrWhiteSpace(clientSecret))
            {
                context.SetError("invalid_clientId", "Client secret should be sent.");
                return Task.FromResult<object>(null);
            }
            else
            {
                if (client.Secret != Helper.GetHash(clientSecret))
                {
                    context.SetError("invalid_clientId", "Client secret is invalid.");
                    return Task.FromResult<object>(null);
                }
            }
        }

        if (!client.Active)
        {
            context.SetError("invalid_clientId", "Client is inactive.");
            return Task.FromResult<object>(null);
        }

        context.OwinContext.Set<string>("as:clientAllowedOrigin", client.AllowedOrigin);
        context.OwinContext.Set<string>("as:clientRefreshTokenLifeTime", client.RefreshTokenLifeTime.ToString());

        context.Validated();
        return Task.FromResult<object>(null);
    }

您还可以查看本教程here

相关问题