Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit d28c6e1

Browse files
committed
Changes for error handling in Authentication
1 parent 0581bcf commit d28c6e1

File tree

7 files changed

+40
-28
lines changed

7 files changed

+40
-28
lines changed

src/Microsoft.AspNet.Http.Abstractions/Authentication/AuthenticationManager.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ namespace Microsoft.AspNet.Http.Authentication
1111
{
1212
public abstract class AuthenticationManager
1313
{
14+
/// <summary>
15+
/// Constant used to represent the automatic scheme
16+
/// </summary>
17+
public const string AutomaticScheme = "Automatic";
18+
1419
public abstract IEnumerable<AuthenticationDescription> GetAuthenticationSchemes();
1520

1621
public abstract Task AuthenticateAsync(AuthenticateContext context);
@@ -34,14 +39,14 @@ public virtual Task ChallengeAsync()
3439

3540
public virtual Task ChallengeAsync(AuthenticationProperties properties)
3641
{
37-
return ChallengeAsync(authenticationScheme: string.Empty, properties: properties);
42+
return ChallengeAsync(authenticationScheme: AutomaticScheme, properties: properties);
3843
}
3944

4045
public virtual Task ChallengeAsync(string authenticationScheme)
4146
{
42-
if (authenticationScheme == null)
47+
if (string.IsNullOrEmpty(authenticationScheme))
4348
{
44-
throw new ArgumentNullException(nameof(authenticationScheme));
49+
throw new ArgumentException(nameof(authenticationScheme));
4550
}
4651

4752
return ChallengeAsync(authenticationScheme: authenticationScheme, properties: null);
@@ -50,19 +55,19 @@ public virtual Task ChallengeAsync(string authenticationScheme)
5055
// Leave it up to authentication handler to do the right thing for the challenge
5156
public virtual Task ChallengeAsync(string authenticationScheme, AuthenticationProperties properties)
5257
{
53-
if (authenticationScheme == null)
58+
if (string.IsNullOrEmpty(authenticationScheme))
5459
{
55-
throw new ArgumentNullException(nameof(authenticationScheme));
60+
throw new ArgumentException(nameof(authenticationScheme));
5661
}
5762

5863
return ChallengeAsync(authenticationScheme, properties, ChallengeBehavior.Automatic);
5964
}
6065

6166
public virtual Task SignInAsync(string authenticationScheme, ClaimsPrincipal principal)
6267
{
63-
if (authenticationScheme == null)
68+
if (string.IsNullOrEmpty(authenticationScheme))
6469
{
65-
throw new ArgumentNullException(nameof(authenticationScheme));
70+
throw new ArgumentException(nameof(authenticationScheme));
6671
}
6772

6873
if (principal == null)

src/Microsoft.AspNet.Http.Features/Authentication/AuthenticateContext.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class AuthenticateContext
1111
{
1212
public AuthenticateContext(string authenticationScheme)
1313
{
14-
if (authenticationScheme == null)
14+
if (string.IsNullOrEmpty(authenticationScheme))
1515
{
16-
throw new ArgumentNullException(nameof(authenticationScheme));
16+
throw new ArgumentException(nameof(authenticationScheme));
1717
}
1818

1919
AuthenticationScheme = authenticationScheme;
@@ -29,6 +29,8 @@ public AuthenticateContext(string authenticationScheme)
2929

3030
public IDictionary<string, object> Description { get; private set; }
3131

32+
public Exception Error { get; private set; }
33+
3234
public virtual void Authenticated(ClaimsPrincipal principal, IDictionary<string, string> properties, IDictionary<string, object> description)
3335
{
3436
Accepted = true;
@@ -41,5 +43,11 @@ public virtual void NotAuthenticated()
4143
{
4244
Accepted = true;
4345
}
46+
47+
public virtual void Failed(Exception error)
48+
{
49+
Error = error;
50+
Accepted = true;
51+
}
4452
}
4553
}

src/Microsoft.AspNet.Http.Features/Authentication/ChallengeContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public ChallengeContext(string authenticationScheme)
1515

1616
public ChallengeContext(string authenticationScheme, IDictionary<string, string> properties, ChallengeBehavior behavior)
1717
{
18-
if (authenticationScheme == null)
18+
if (string.IsNullOrEmpty(authenticationScheme))
1919
{
20-
throw new ArgumentNullException(nameof(authenticationScheme));
20+
throw new ArgumentException(nameof(authenticationScheme));
2121
}
2222

2323
AuthenticationScheme = authenticationScheme;

src/Microsoft.AspNet.Http.Features/Authentication/SignInContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class SignInContext
1111
{
1212
public SignInContext(string authenticationScheme, ClaimsPrincipal principal, IDictionary<string, string> properties)
1313
{
14-
if (authenticationScheme == null)
14+
if (string.IsNullOrEmpty(authenticationScheme))
1515
{
16-
throw new ArgumentNullException(nameof(authenticationScheme));
16+
throw new ArgumentException(nameof(authenticationScheme));
1717
}
1818

1919
if (principal == null)

src/Microsoft.AspNet.Http.Features/Authentication/SignOutContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public class SignOutContext
1010
{
1111
public SignOutContext(string authenticationScheme, IDictionary<string, string> properties)
1212
{
13-
if (authenticationScheme == null)
13+
if (string.IsNullOrEmpty(authenticationScheme))
1414
{
15-
throw new ArgumentNullException(nameof(authenticationScheme));
15+
throw new ArgumentException(nameof(authenticationScheme));
1616
}
1717

1818
AuthenticationScheme = authenticationScheme;

src/Microsoft.AspNet.Http/Authentication/DefaultAuthenticationManager.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,22 @@ public override async Task AuthenticateAsync(AuthenticateContext context)
5454
}
5555

5656
var handler = HttpAuthenticationFeature.Handler;
57-
5857
if (handler != null)
5958
{
6059
await handler.AuthenticateAsync(context);
6160
}
6261

6362
if (!context.Accepted)
6463
{
65-
throw new InvalidOperationException($"The following authentication scheme was not accepted: {context.AuthenticationScheme}");
64+
throw new InvalidOperationException($"No authentication handler is configured to authenticate for the scheme: {context.AuthenticationScheme}");
6665
}
6766
}
6867

6968
public override async Task ChallengeAsync(string authenticationScheme, AuthenticationProperties properties, ChallengeBehavior behavior)
7069
{
71-
if (authenticationScheme == null)
70+
if (string.IsNullOrEmpty(authenticationScheme))
7271
{
73-
throw new ArgumentNullException(nameof(authenticationScheme));
72+
throw new ArgumentException(nameof(authenticationScheme));
7473
}
7574

7675
var handler = HttpAuthenticationFeature.Handler;
@@ -83,15 +82,15 @@ public override async Task ChallengeAsync(string authenticationScheme, Authentic
8382

8483
if (!challengeContext.Accepted)
8584
{
86-
throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}");
85+
throw new InvalidOperationException($"No authentication handler is configured to handle the scheme: {authenticationScheme}");
8786
}
8887
}
8988

9089
public override async Task SignInAsync(string authenticationScheme, ClaimsPrincipal principal, AuthenticationProperties properties)
9190
{
92-
if (authenticationScheme == null)
91+
if (string.IsNullOrEmpty(authenticationScheme))
9392
{
94-
throw new ArgumentNullException(nameof(authenticationScheme));
93+
throw new ArgumentException(nameof(authenticationScheme));
9594
}
9695

9796
if (principal == null)
@@ -109,15 +108,15 @@ public override async Task SignInAsync(string authenticationScheme, ClaimsPrinci
109108

110109
if (!signInContext.Accepted)
111110
{
112-
throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}");
111+
throw new InvalidOperationException($"No authentication handler is configured to handle the scheme: {authenticationScheme}");
113112
}
114113
}
115114

116115
public override async Task SignOutAsync(string authenticationScheme, AuthenticationProperties properties)
117116
{
118-
if (authenticationScheme == null)
117+
if (string.IsNullOrEmpty(authenticationScheme))
119118
{
120-
throw new ArgumentNullException(nameof(authenticationScheme));
119+
throw new ArgumentException(nameof(authenticationScheme));
121120
}
122121

123122
var handler = HttpAuthenticationFeature.Handler;
@@ -130,7 +129,7 @@ public override async Task SignOutAsync(string authenticationScheme, Authenticat
130129

131130
if (!signOutContext.Accepted)
132131
{
133-
throw new InvalidOperationException($"The following authentication scheme was not accepted: {authenticationScheme}");
132+
throw new InvalidOperationException($"No authentication handler is configured to handle the scheme: {authenticationScheme}");
134133
}
135134
}
136135
}

test/Microsoft.AspNet.Http.Tests/Authentication/DefaultAuthenticationManagerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Microsoft.AspNet.Http.Authentication.Internal
1414
{
15-
public class AuthenticationManagerTests
15+
public class DefaultAuthenticationManagerTests
1616
{
1717

1818
[Fact]
@@ -23,7 +23,7 @@ public async Task AuthenticateWithNoAuthMiddlewareThrows()
2323
}
2424

2525
[Theory]
26-
[InlineData("")]
26+
[InlineData("Automatic")]
2727
[InlineData("Foo")]
2828
public async Task ChallengeWithNoAuthMiddlewareMayThrow(string scheme)
2929
{

0 commit comments

Comments
 (0)