#34 - Make HttpContext.User return non-null.
This commit is contained in:
parent
66495cdc58
commit
b751cf19d0
|
|
@ -54,7 +54,16 @@ namespace Microsoft.AspNet.PipelineCore
|
||||||
|
|
||||||
public override ClaimsPrincipal User
|
public override ClaimsPrincipal User
|
||||||
{
|
{
|
||||||
get { return HttpAuthentication.User; }
|
get
|
||||||
|
{
|
||||||
|
var user = HttpAuthentication.User;
|
||||||
|
if (user == null)
|
||||||
|
{
|
||||||
|
user = new ClaimsPrincipal(new ClaimsIdentity());
|
||||||
|
HttpAuthentication.User = user;
|
||||||
|
}
|
||||||
|
return user;
|
||||||
|
}
|
||||||
set { HttpAuthentication.User = value; }
|
set { HttpAuthentication.User = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.AspNet.Abstractions;
|
||||||
|
using Microsoft.AspNet.FeatureModel;
|
||||||
|
using Microsoft.AspNet.HttpFeature;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.PipelineCore.Tests
|
||||||
|
{
|
||||||
|
public class DefaultHttpContextTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void EmptyUserIsNeverNull()
|
||||||
|
{
|
||||||
|
var context = new DefaultHttpContext(new FeatureCollection());
|
||||||
|
Assert.NotNull(context.User);
|
||||||
|
Assert.Equal(1, context.User.Identities.Count());
|
||||||
|
Assert.True(object.ReferenceEquals(context.User, context.User));
|
||||||
|
Assert.False(context.User.Identity.IsAuthenticated);
|
||||||
|
Assert.True(string.IsNullOrEmpty(context.User.Identity.AuthenticationType));
|
||||||
|
|
||||||
|
context.User = null;
|
||||||
|
Assert.NotNull(context.User);
|
||||||
|
Assert.Equal(1, context.User.Identities.Count());
|
||||||
|
Assert.True(object.ReferenceEquals(context.User, context.User));
|
||||||
|
Assert.False(context.User.Identity.IsAuthenticated);
|
||||||
|
Assert.True(string.IsNullOrEmpty(context.User.Identity.AuthenticationType));
|
||||||
|
|
||||||
|
context.User = new ClaimsPrincipal();
|
||||||
|
Assert.NotNull(context.User);
|
||||||
|
Assert.Equal(0, context.User.Identities.Count());
|
||||||
|
Assert.True(object.ReferenceEquals(context.User, context.User));
|
||||||
|
Assert.Null(context.User.Identity);
|
||||||
|
|
||||||
|
context.User = new ClaimsPrincipal(new ClaimsIdentity("SomeAuthType"));
|
||||||
|
Assert.Equal("SomeAuthType", context.User.Identity.AuthenticationType);
|
||||||
|
Assert.True(context.User.Identity.IsAuthenticated);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -24,6 +24,7 @@
|
||||||
<Compile Include="BuilderTests.cs" />
|
<Compile Include="BuilderTests.cs" />
|
||||||
<Compile Include="DefaultCanHasFormTests.cs" />
|
<Compile Include="DefaultCanHasFormTests.cs" />
|
||||||
<Compile Include="DefaultCanHasQueryTests.cs" />
|
<Compile Include="DefaultCanHasQueryTests.cs" />
|
||||||
|
<Compile Include="DefaultHttpContextTests.cs" />
|
||||||
<Compile Include="DefaultHttpRequestTests.cs" />
|
<Compile Include="DefaultHttpRequestTests.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue