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

Commit

Permalink
Make IHttpContextAccessor Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoK committed Jan 7, 2016
1 parent 042588e commit 167bb54
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
25 changes: 22 additions & 3 deletions src/Microsoft.AspNet.Identity/SignInManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public SignInManager(UserManager<TUser> userManager,
{
throw new ArgumentNullException(nameof(userManager));
}
if (contextAccessor == null || contextAccessor.HttpContext == null)
if (contextAccessor == null)
{
throw new ArgumentNullException(nameof(contextAccessor));
}
Expand All @@ -51,12 +51,15 @@ public SignInManager(UserManager<TUser> userManager,
}

UserManager = userManager;
Context = contextAccessor.HttpContext;
_contextAccessor = contextAccessor;
ClaimsFactory = claimsFactory;
Options = optionsAccessor?.Value ?? new IdentityOptions();
Logger = logger;
}

private readonly IHttpContextAccessor _contextAccessor;
private HttpContext _context;

/// <summary>
/// Gets the <see cref="ILogger"/> used to log messages from the manager.
/// </summary>
Expand All @@ -65,10 +68,26 @@ public SignInManager(UserManager<TUser> userManager,
/// </value>
protected internal virtual ILogger Logger { get; set; }
protected internal UserManager<TUser> UserManager { get; set; }
internal HttpContext Context { get; set; }
internal IUserClaimsPrincipalFactory<TUser> ClaimsFactory { get; set; }
internal IdentityOptions Options { get; set; }

internal HttpContext Context {
get
{
var context = _context ?? _contextAccessor?.HttpContext;
if (context == null)
{
throw new InvalidOperationException("HttpContext must not be null.");
}
return context;
}
set
{
_context = value;
}
}


/// <summary>
/// Creates a <see cref="ClaimsPrincipal"/> for the specified <paramref name="user"/>, as an asynchronous operation.
/// </summary>
Expand Down
5 changes: 2 additions & 3 deletions src/Microsoft.AspNet.Identity/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,14 @@ public UserManager(IUserStore<TUser> store,
ILookupNormalizer keyNormalizer,
IdentityErrorDescriber errors,
IServiceProvider services,
ILogger<UserManager<TUser>> logger,
IHttpContextAccessor contextAccessor)
ILogger<UserManager<TUser>> logger)
{
if (store == null)
{
throw new ArgumentNullException(nameof(store));
}
Store = store;
Options = optionsAccessor?.Value ?? new IdentityOptions();
_context = contextAccessor?.HttpContext;
PasswordHasher = passwordHasher;
KeyNormalizer = keyNormalizer;
ErrorDescriber = errors;
Expand All @@ -86,6 +84,7 @@ public UserManager(IUserStore<TUser> store,

if (services != null)
{
_context = services.GetService<IHttpContextAccessor>()?.HttpContext;
foreach (var providerName in Options.Tokens.ProviderMap.Keys)
{
var provider = services.GetRequiredService(Options.Tokens.ProviderMap[providerName].ProviderType) as IUserTokenProvider<TUser>;
Expand Down
2 changes: 1 addition & 1 deletion test/Microsoft.AspNet.Identity.Test/IdentityBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ Task<TestRole> IRoleStore<TestRole>.FindByNameAsync(string roleName, Cancellatio

private class MyUserManager : UserManager<TestUser>
{
public MyUserManager(IUserStore<TestUser> store) : base(store, null, null, null, null, null, null, null, null, null) { }
public MyUserManager(IUserStore<TestUser> store) : base(store, null, null, null, null, null, null, null, null) { }
}

private class MyRoleManager : RoleManager<TestRole>
Expand Down
1 change: 0 additions & 1 deletion test/Microsoft.AspNet.Identity.Test/SignInManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public void ConstructorNullChecks()
var userManager = MockHelpers.MockUserManager<TestUser>().Object;
Assert.Throws<ArgumentNullException>("contextAccessor", () => new SignInManager<TestUser>(userManager, null, null, null, null));
var contextAccessor = new Mock<IHttpContextAccessor>();
Assert.Throws<ArgumentNullException>("contextAccessor", () => new SignInManager<TestUser>(userManager, contextAccessor.Object, null, null, null));
var context = new Mock<HttpContext>();
contextAccessor.Setup(a => a.HttpContext).Returns(context.Object);
Assert.Throws<ArgumentNullException>("claimsFactory", () => new SignInManager<TestUser>(userManager, contextAccessor.Object, null, null, null));
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.AspNet.Identity.Test/UserManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void AddUserManagerWithCustomManagerReturnsSameInstance()

public class CustomUserManager : UserManager<TestUser>
{
public CustomUserManager() : base(new Mock<IUserStore<TestUser>>().Object, null, null, null, null, null, null, null, null, null)
public CustomUserManager() : base(new Mock<IUserStore<TestUser>>().Object, null, null, null, null, null, null, null, null)
{ }
}

Expand Down Expand Up @@ -694,7 +694,7 @@ public async Task ResetTokenCallNoopForTokenValueZero()
public async Task ManagerPublicNullChecks()
{
Assert.Throws<ArgumentNullException>("store",
() => new UserManager<TestUser>(null, null, null, null, null, null, null, null, null, null));
() => new UserManager<TestUser>(null, null, null, null, null, null, null, null, null));

var manager = MockHelpers.TestUserManager(new NotImplementedStore());

Expand Down
5 changes: 2 additions & 3 deletions test/Shared/MockHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class MockHelpers
public static Mock<UserManager<TUser>> MockUserManager<TUser>() where TUser : class
{
var store = new Mock<IUserStore<TUser>>();
var mgr = new Mock<UserManager<TUser>>(store.Object, null, null, null, null, null, null, null, null, null);
var mgr = new Mock<UserManager<TUser>>(store.Object, null, null, null, null, null, null, null, null);
mgr.Object.UserValidators.Add(new UserValidator<TUser>());
mgr.Object.PasswordValidators.Add(new PasswordValidator<TUser>());
return mgr;
Expand Down Expand Up @@ -76,8 +76,7 @@ public static UserManager<TUser> TestUserManager<TUser>(IUserStore<TUser> store
var userManager = new UserManager<TUser>(store, options.Object, new PasswordHasher<TUser>(),
userValidators, pwdValidators, new UpperInvariantLookupNormalizer(),
new IdentityErrorDescriber(), null,
new Mock<ILogger<UserManager<TUser>>>().Object,
null);
new Mock<ILogger<UserManager<TUser>>>().Object);
validator.Setup(v => v.ValidateAsync(userManager, It.IsAny<TUser>()))
.Returns(Task.FromResult(IdentityResult.Success)).Verifiable();
return userManager;
Expand Down

0 comments on commit 167bb54

Please sign in to comment.