|
1 | 1 | // Copyright (c) .NET Foundation. All rights reserved.
|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
3 | 3 |
|
| 4 | +using System; |
4 | 5 | using System.Collections.Generic;
|
5 | 6 | using System.Linq;
|
| 7 | +using System.Security.Claims; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.AspNetCore.Http; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
6 | 11 | using Xunit;
|
7 | 12 |
|
8 | 13 | namespace Microsoft.AspNetCore.Authentication
|
@@ -117,7 +122,70 @@ public void UpdateTokenValueReturnsFalseForUnknownToken()
|
117 | 122 | Assert.Null(props.GetTokenValue("ONE"));
|
118 | 123 | Assert.Null(props.GetTokenValue("Jigglypuff"));
|
119 | 124 | Assert.Equal(3, props.GetTokens().Count());
|
| 125 | + } |
120 | 126 |
|
| 127 | + [Fact] |
| 128 | + public async Task GetTokenWorksWithDefaultAuthenticateScheme() |
| 129 | + { |
| 130 | + var context = new DefaultHttpContext(); |
| 131 | + var services = new ServiceCollection().AddOptions() |
| 132 | + .AddAuthenticationCore(o => o.AddScheme("simple", s => s.HandlerType = typeof(SimpleAuth))); |
| 133 | + context.RequestServices = services.BuildServiceProvider(); |
| 134 | + |
| 135 | + Assert.Equal("1", await context.GetTokenAsync("One")); |
| 136 | + Assert.Equal("2", await context.GetTokenAsync("Two")); |
| 137 | + Assert.Equal("3", await context.GetTokenAsync("Three")); |
121 | 138 | }
|
| 139 | + |
| 140 | + [Fact] |
| 141 | + public async Task GetTokenWorksWithExplicitScheme() |
| 142 | + { |
| 143 | + var context = new DefaultHttpContext(); |
| 144 | + var services = new ServiceCollection().AddOptions() |
| 145 | + .AddAuthenticationCore(o => o.AddScheme("simple", s => s.HandlerType = typeof(SimpleAuth))); |
| 146 | + context.RequestServices = services.BuildServiceProvider(); |
| 147 | + |
| 148 | + Assert.Equal("1", await context.GetTokenAsync("simple", "One")); |
| 149 | + Assert.Equal("2", await context.GetTokenAsync("simple", "Two")); |
| 150 | + Assert.Equal("3", await context.GetTokenAsync("simple", "Three")); |
| 151 | + } |
| 152 | + |
| 153 | + private class SimpleAuth : IAuthenticationHandler |
| 154 | + { |
| 155 | + public Task<AuthenticateResult> AuthenticateAsync() |
| 156 | + { |
| 157 | + var props = new AuthenticationProperties(); |
| 158 | + var tokens = new List<AuthenticationToken>(); |
| 159 | + var tok1 = new AuthenticationToken { Name = "One", Value = "1" }; |
| 160 | + var tok2 = new AuthenticationToken { Name = "Two", Value = "2" }; |
| 161 | + var tok3 = new AuthenticationToken { Name = "Three", Value = "3" }; |
| 162 | + tokens.Add(tok1); |
| 163 | + tokens.Add(tok2); |
| 164 | + tokens.Add(tok3); |
| 165 | + props.StoreTokens(tokens); |
| 166 | + return Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(new ClaimsPrincipal(), props, "simple"))); |
| 167 | + } |
| 168 | + |
| 169 | + public Task ChallengeAsync(ChallengeContext context) |
| 170 | + { |
| 171 | + return Task.FromResult(0); |
| 172 | + } |
| 173 | + |
| 174 | + public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context) |
| 175 | + { |
| 176 | + return Task.FromResult(0); |
| 177 | + } |
| 178 | + |
| 179 | + public Task SignInAsync(SignInContext context) |
| 180 | + { |
| 181 | + return Task.FromResult(0); |
| 182 | + } |
| 183 | + |
| 184 | + public Task SignOutAsync(SignOutContext context) |
| 185 | + { |
| 186 | + return Task.FromResult(0); |
| 187 | + } |
| 188 | + } |
| 189 | + |
122 | 190 | }
|
123 | 191 | }
|
0 commit comments