// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Security
{
public static class AuthorizationServiceExtensions
{
///
/// Checks if a user has specific claims.
///
/// The claim to check against a specific user.
/// The user to check claims against.
/// true when the user fulfills one of the claims, false otherwise.
public static Task AuthorizeAsync([NotNull] this IAuthorizationService service, Claim claim, ClaimsPrincipal user)
{
return service.AuthorizeAsync(new Claim[] { claim }, user);
}
///
/// Checks if a user has specific claims.
///
/// The claim to check against a specific user.
/// The user to check claims against.
/// true when the user fulfills one of the claims, false otherwise.
public static bool Authorize([NotNull] this IAuthorizationService service, Claim claim, ClaimsPrincipal user)
{
return service.Authorize(new Claim[] { claim }, user);
}
///
/// Checks if a user has specific claims for a specific context obj.
///
/// The claim to check against a specific user.
/// The user to check claims against.
/// The resource the claims should be check with.
/// true when the user fulfills one of the claims, false otherwise.
public static Task AuthorizeAsync([NotNull] this IAuthorizationService service, Claim claim, ClaimsPrincipal user, object resource)
{
return service.AuthorizeAsync(new Claim[] { claim }, user, resource);
}
///
/// Checks if a user has specific claims for a specific context obj.
///
/// The claimsto check against a specific user.
/// The user to check claims against.
/// The resource the claims should be check with.
/// true when the user fulfills one of the claims, false otherwise.
public static bool Authorize([NotNull] this IAuthorizationService service, Claim claim, ClaimsPrincipal user, object resource)
{
return service.Authorize(new Claim[] { claim }, user, resource);
}
///
/// Checks if a user has specific claims.
///
/// The claims to check against a specific user.
/// The user to check claims against.
/// true when the user fulfills one of the claims, false otherwise.
public static Task AuthorizeAsync([NotNull] this IAuthorizationService service, IEnumerable claims, ClaimsPrincipal user)
{
return service.AuthorizeAsync(claims, user, null);
}
///
/// Checks if a user has specific claims.
///
/// The claims to check against a specific user.
/// The user to check claims against.
/// true when the user fulfills one of the claims, false otherwise.
public static bool Authorize([NotNull] this IAuthorizationService service, IEnumerable claims, ClaimsPrincipal user)
{
return service.Authorize(claims, user, null);
}
}
}