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

Commit

Permalink
Fix GetToken()
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoK committed Apr 24, 2017
1 parent 5472763 commit 3085f01
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ public static async Task<string> GetTokenAsync(this IAuthenticationService auth,
{
throw new ArgumentNullException(nameof(auth));
}
if (scheme == null)
{
throw new ArgumentNullException(nameof(scheme));
}
if (tokenName == null)
{
throw new ArgumentNullException(nameof(tokenName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Authentication.Core\Microsoft.AspNetCore.Authentication.Core.csproj" />
<PackageReference Include="Microsoft.AspNetCore.Testing" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Microsoft.AspNetCore.Authentication
Expand Down Expand Up @@ -117,7 +122,70 @@ public void UpdateTokenValueReturnsFalseForUnknownToken()
Assert.Null(props.GetTokenValue("ONE"));
Assert.Null(props.GetTokenValue("Jigglypuff"));
Assert.Equal(3, props.GetTokens().Count());
}

[Fact]
public async Task GetTokenWorksWithDefaultAuthenticateScheme()
{
var context = new DefaultHttpContext();
var services = new ServiceCollection().AddOptions()
.AddAuthenticationCore(o => o.AddScheme("simple", s => s.HandlerType = typeof(SimpleAuth)));
context.RequestServices = services.BuildServiceProvider();

Assert.Equal("1", await context.GetTokenAsync("One"));
Assert.Equal("2", await context.GetTokenAsync("Two"));
Assert.Equal("3", await context.GetTokenAsync("Three"));
}

[Fact]
public async Task GetTokenWorksWithExplicitScheme()
{
var context = new DefaultHttpContext();
var services = new ServiceCollection().AddOptions()
.AddAuthenticationCore(o => o.AddScheme("simple", s => s.HandlerType = typeof(SimpleAuth)));
context.RequestServices = services.BuildServiceProvider();

Assert.Equal("1", await context.GetTokenAsync("simple", "One"));
Assert.Equal("2", await context.GetTokenAsync("simple", "Two"));
Assert.Equal("3", await context.GetTokenAsync("simple", "Three"));
}

private class SimpleAuth : IAuthenticationHandler
{
public Task<AuthenticateResult> AuthenticateAsync()
{
var props = new AuthenticationProperties();
var tokens = new List<AuthenticationToken>();
var tok1 = new AuthenticationToken { Name = "One", Value = "1" };
var tok2 = new AuthenticationToken { Name = "Two", Value = "2" };
var tok3 = new AuthenticationToken { Name = "Three", Value = "3" };
tokens.Add(tok1);
tokens.Add(tok2);
tokens.Add(tok3);
props.StoreTokens(tokens);
return Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(new ClaimsPrincipal(), props, "simple")));
}

public Task ChallengeAsync(ChallengeContext context)
{
return Task.FromResult(0);
}

public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
{
return Task.FromResult(0);
}

public Task SignInAsync(SignInContext context)
{
return Task.FromResult(0);
}

public Task SignOutAsync(SignOutContext context)
{
return Task.FromResult(0);
}
}

}
}

0 comments on commit 3085f01

Please sign in to comment.