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

Commit 3085f01

Browse files
committed
Fix GetToken()
1 parent 5472763 commit 3085f01

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

src/Microsoft.AspNetCore.Authentication.Abstractions/TokenExtensions.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,6 @@ public static async Task<string> GetTokenAsync(this IAuthenticationService auth,
149149
{
150150
throw new ArgumentNullException(nameof(auth));
151151
}
152-
if (scheme == null)
153-
{
154-
throw new ArgumentNullException(nameof(scheme));
155-
}
156152
if (tokenName == null)
157153
{
158154
throw new ArgumentNullException(nameof(tokenName));

test/Microsoft.AspNetCore.Authentication.Core.Test/Microsoft.AspNetCore.Authentication.Core.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<ItemGroup>
99
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Authentication.Core\Microsoft.AspNetCore.Authentication.Core.csproj" />
1010
<PackageReference Include="Microsoft.AspNetCore.Testing" Version="$(AspNetCoreVersion)" />
11+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(AspNetCoreVersion)" />
1112
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
1213
<PackageReference Include="xunit" Version="$(XunitVersion)" />
1314
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" />

test/Microsoft.AspNetCore.Authentication.Core.Test/TokenExtensionTests.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
7+
using System.Security.Claims;
8+
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Http;
10+
using Microsoft.Extensions.DependencyInjection;
611
using Xunit;
712

813
namespace Microsoft.AspNetCore.Authentication
@@ -117,7 +122,70 @@ public void UpdateTokenValueReturnsFalseForUnknownToken()
117122
Assert.Null(props.GetTokenValue("ONE"));
118123
Assert.Null(props.GetTokenValue("Jigglypuff"));
119124
Assert.Equal(3, props.GetTokens().Count());
125+
}
120126

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"));
121138
}
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+
122190
}
123191
}

0 commit comments

Comments
 (0)