예제 #1
0
        internal static async Task <bool> IsAuthenticatedAsync(this HttpContext context, SharedOptionsBase options)
        {
            if (options.AllowAnonymous)
            {
                return(true);
            }

            var authSchemes = options.AuthenticationSchemes.Any()
                                  ? options.AuthenticationSchemes
                                  : context.RequestServices.GetService <IAuthenticationSchemeProvider>()
                              .GetAllSchemesAsync().Result.Select(scheme => scheme.Name).ToArray();

            foreach (var authScheme in authSchemes)
            {
                var cp = await context.AuthenticateAsync(authScheme);

                if (cp == null || !cp.Succeeded)
                {
                    continue;
                }
                context.User = cp.Principal;
                break;
            }
            return(context.User != null && context.User.Identity.IsAuthenticated);
        }
예제 #2
0
        internal static async Task <bool> IsAuthorizedAsync(this HttpContext context, SharedOptionsBase options, ILibrary library, string policy)
        {
            var authService    = context.RequestServices.GetService <IAuthorizationService>();
            var policyProvider = context.RequestServices.GetService <IAuthorizationPolicyProvider>();

            return(options.AllowAnonymous ||
                   await policyProvider.GetPolicyAsync(policy) == null ||
                   (await authService.AuthorizeAsync(context.User, new LibraryServerAuthorizationResource(context, library), policy)).Succeeded);
        }
예제 #3
0
 internal static bool IsAuthenticated(this HttpContext context, SharedOptionsBase options) =>
 IsAuthenticatedAsync(context, options).Result;
예제 #4
0
 internal static bool IsAuthorized(this HttpContext context, SharedOptionsBase options, ILibrary library, string policy) =>
 IsAuthorizedAsync(context, options, library, policy).Result;