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

Commit 6ba7793

Browse files
committed
Merge pull request #376 from khellang/owin-request-user
Add owin.RequestUser
2 parents d013bdf + 138bc6a commit 6ba7793

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

src/Microsoft.AspNet.Owin/OwinConstants.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ internal static class OwinConstants
2020

2121
#endregion
2222

23-
#region OWIN v1.1.0 - 3.2.1 Request Data
23+
#region OWIN v1.0.1 - 3.2.1 Request Data
2424

25-
// OWIN 1.1.0 http://owin.org/html/owin.html
25+
// OWIN 1.0.1 http://owin.org/html/owin.html
2626

2727
public const string RequestId = "owin.RequestId";
28+
public const string RequestUser = "owin.RequestUser";
2829

2930
#endregion
3031

src/Microsoft.AspNet.Owin/OwinEnvironment.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Linq;
1010
using System.Net;
1111
using System.Net.WebSockets;
12+
using System.Security.Claims;
1213
using System.Security.Cryptography.X509Certificates;
1314
using System.Security.Principal;
1415
using System.Threading;
@@ -57,6 +58,7 @@ public OwinEnvironment(HttpContext context)
5758
(feature, value) => feature.QueryString = Utilities.AddQuestionMark(Convert.ToString(value))) },
5859
{ OwinConstants.RequestHeaders, new FeatureMap<IHttpRequestFeature>(feature => feature.Headers, (feature, value) => feature.Headers = (IDictionary<string, string[]>)value) },
5960
{ OwinConstants.RequestBody, new FeatureMap<IHttpRequestFeature>(feature => feature.Body, () => Stream.Null, (feature, value) => feature.Body = (Stream)value) },
61+
{ OwinConstants.RequestUser, new FeatureMap<IHttpAuthenticationFeature>(feature => feature.User, () => null, (feature, value) => feature.User = (ClaimsPrincipal)value) },
6062

6163
{ OwinConstants.ResponseStatusCode, new FeatureMap<IHttpResponseFeature>(feature => feature.StatusCode, () => 200, (feature, value) => feature.StatusCode = Convert.ToInt32(value)) },
6264
{ OwinConstants.ResponseReasonPhrase, new FeatureMap<IHttpResponseFeature>(feature => feature.ReasonPhrase, (feature, value) => feature.ReasonPhrase = Convert.ToString(value)) },

src/Microsoft.AspNet.Owin/OwinFeatureCollection.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,16 @@ void IHttpRequestLifetimeFeature.Abort()
263263

264264
ClaimsPrincipal IHttpAuthenticationFeature.User
265265
{
266-
get { return Utilities.MakeClaimsPrincipal(Prop<IPrincipal>(OwinConstants.Security.User)); }
267-
set { Prop(OwinConstants.Security.User, value); }
266+
get
267+
{
268+
return Prop<ClaimsPrincipal>(OwinConstants.RequestUser)
269+
?? Utilities.MakeClaimsPrincipal(Prop<IPrincipal>(OwinConstants.Security.User));
270+
}
271+
set
272+
{
273+
Prop(OwinConstants.RequestUser, value);
274+
Prop(OwinConstants.Security.User, value);
275+
}
268276
}
269277

270278
IAuthenticationHandler IHttpAuthenticationFeature.Handler { get; set; }

test/Microsoft.AspNet.Owin.Tests/OwinEnvironmentTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public void OwinEnvironmentCanBeCreated()
3939

4040
IDictionary<string, object> env = new OwinEnvironment(context);
4141
Assert.Equal("SomeMethod", Get<string>(env, "owin.RequestMethod"));
42+
// User property should set both server.User (non-standard) and owin.RequestUser.
4243
Assert.Equal("Foo", Get<ClaimsPrincipal>(env, "server.User").Identity.AuthenticationType);
44+
Assert.Equal("Foo", Get<ClaimsPrincipal>(env, "owin.RequestUser").Identity.AuthenticationType);
4345
Assert.Same(Stream.Null, Get<Stream>(env, "owin.RequestBody"));
4446
var requestHeaders = Get<IDictionary<string, string[]>>(env, "owin.RequestHeaders");
4547
Assert.NotNull(requestHeaders);
@@ -65,6 +67,10 @@ public void OwinEnvironmentCanBeModified()
6567

6668
env["owin.RequestMethod"] = "SomeMethod";
6769
env["server.User"] = new ClaimsPrincipal(new ClaimsIdentity("Foo"));
70+
Assert.Equal("Foo", context.User.Identity.AuthenticationType);
71+
// User property should fall back from owin.RequestUser to server.User.
72+
env["owin.RequestUser"] = new ClaimsPrincipal(new ClaimsIdentity("Bar"));
73+
Assert.Equal("Bar", context.User.Identity.AuthenticationType);
6874
env["owin.RequestBody"] = Stream.Null;
6975
var requestHeaders = Get<IDictionary<string, string[]>>(env, "owin.RequestHeaders");
7076
Assert.NotNull(requestHeaders);
@@ -81,7 +87,6 @@ public void OwinEnvironmentCanBeModified()
8187
env["owin.ResponseStatusCode"] = 201;
8288

8389
Assert.Equal("SomeMethod", context.Request.Method);
84-
Assert.Equal("Foo", context.User.Identity.AuthenticationType);
8590
Assert.Same(Stream.Null, context.Request.Body);
8691
Assert.Equal("CustomRequestValue", context.Request.Headers["CustomRequestHeader"]);
8792
Assert.Equal("/path", context.Request.Path.Value);

0 commit comments

Comments
 (0)