From e2a8efbb64578602828336474d7083e5f7842702 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Tue, 17 Mar 2015 11:40:58 -0700 Subject: [PATCH] Cleanup Switch to logging interfaces reference Tweak DenyAnonymous logic Fixes https://github.com/aspnet/Security/issues/181 Fixes https://github.com/aspnet/Security/issues/169 --- src/Microsoft.AspNet.Authentication/project.json | 2 +- .../DefaultAuthorizationService.cs | 16 ++++++---------- .../DenyAnonymousAuthorizationHandler.cs | 7 +++---- src/Microsoft.AspNet.Authorization/project.json | 2 +- .../DefaultAuthorizationServiceTests.cs | 13 ++++++------- 5 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/Microsoft.AspNet.Authentication/project.json b/src/Microsoft.AspNet.Authentication/project.json index 609f641adb..81b89148df 100644 --- a/src/Microsoft.AspNet.Authentication/project.json +++ b/src/Microsoft.AspNet.Authentication/project.json @@ -6,7 +6,7 @@ "Microsoft.AspNet.RequestContainer": "1.0.0-*", "Microsoft.AspNet.Http.Interfaces": "1.0.0-*", "Microsoft.AspNet.Http.Core": "1.0.0-*", - "Microsoft.Framework.Logging": "1.0.0-*", + "Microsoft.Framework.Logging.Interfaces": "1.0.0-*", "Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" } }, "frameworks": { diff --git a/src/Microsoft.AspNet.Authorization/DefaultAuthorizationService.cs b/src/Microsoft.AspNet.Authorization/DefaultAuthorizationService.cs index d99f778d94..c6e57eb549 100644 --- a/src/Microsoft.AspNet.Authorization/DefaultAuthorizationService.cs +++ b/src/Microsoft.AspNet.Authorization/DefaultAuthorizationService.cs @@ -23,11 +23,9 @@ namespace Microsoft.AspNet.Authorization public bool Authorize(ClaimsPrincipal user, object resource, string policyName) { var policy = _options.GetPolicy(policyName); - if (policy == null) - { - return false; - } - return this.Authorize(user, resource, policy); + return (policy == null) + ? false + : this.Authorize(user, resource, policy); } public bool Authorize(ClaimsPrincipal user, object resource, params IAuthorizationRequirement[] requirements) @@ -53,11 +51,9 @@ namespace Microsoft.AspNet.Authorization public Task AuthorizeAsync(ClaimsPrincipal user, object resource, string policyName) { var policy = _options.GetPolicy(policyName); - if (policy == null) - { - return Task.FromResult(false); - } - return this.AuthorizeAsync(user, resource, policy); + return (policy == null) + ? Task.FromResult(false) + : this.AuthorizeAsync(user, resource, policy); } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authorization/DenyAnonymousAuthorizationHandler.cs b/src/Microsoft.AspNet.Authorization/DenyAnonymousAuthorizationHandler.cs index 4ce5d43ed3..0f6cb3def2 100644 --- a/src/Microsoft.AspNet.Authorization/DenyAnonymousAuthorizationHandler.cs +++ b/src/Microsoft.AspNet.Authorization/DenyAnonymousAuthorizationHandler.cs @@ -1,7 +1,7 @@ // 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.Threading.Tasks; +using System.Linq; namespace Microsoft.AspNet.Authorization { @@ -11,9 +11,8 @@ namespace Microsoft.AspNet.Authorization { var user = context.User; var userIsAnonymous = - user == null || - user.Identity == null || - !user.Identity.IsAuthenticated; + user?.Identity == null || + !user.Identities.Any(i => i.IsAuthenticated); if (!userIsAnonymous) { context.Succeed(requirement); diff --git a/src/Microsoft.AspNet.Authorization/project.json b/src/Microsoft.AspNet.Authorization/project.json index 3a8701e86d..6ebb367aa1 100644 --- a/src/Microsoft.AspNet.Authorization/project.json +++ b/src/Microsoft.AspNet.Authorization/project.json @@ -3,7 +3,7 @@ "description": "ASP.NET 5 authorization classes.", "dependencies": { "Microsoft.AspNet.Http.Interfaces": "1.0.0-*", - "Microsoft.Framework.Logging": "1.0.0-*", + "Microsoft.Framework.Logging.Interfaces": "1.0.0-*", "Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" }, "Microsoft.Framework.OptionsModel": "1.0.0-*" }, diff --git a/test/Microsoft.AspNet.Authorization.Test/DefaultAuthorizationServiceTests.cs b/test/Microsoft.AspNet.Authorization.Test/DefaultAuthorizationServiceTests.cs index 4b9b5a33de..74328a956e 100644 --- a/test/Microsoft.AspNet.Authorization.Test/DefaultAuthorizationServiceTests.cs +++ b/test/Microsoft.AspNet.Authorization.Test/DefaultAuthorizationServiceTests.cs @@ -549,13 +549,12 @@ namespace Microsoft.AspNet.Authorization.Test options.AddPolicy("Any", policy => policy.RequireAuthenticatedUser()); }); }); - var user = new ClaimsPrincipal( - new ClaimsIdentity( - new Claim[] { - new Claim(ClaimTypes.Name, "Name"), - }, - "AuthType") - ); + var user = new ClaimsPrincipal(new ClaimsIdentity()); + user.AddIdentity(new ClaimsIdentity( + new Claim[] { + new Claim(ClaimTypes.Name, "Name"), + }, + "AuthType")); // Act var allowed = await authorizationService.AuthorizeAsync(user, null, "Any");