8
8
using System . IO ;
9
9
using System . Linq ;
10
10
using System . Net ;
11
+ using System . Security . Claims ;
11
12
using System . Security . Cryptography . X509Certificates ;
13
+ using System . Security . Principal ;
12
14
using System . Threading ;
13
15
using System . Threading . Tasks ;
14
16
using Microsoft . AspNet . Http ;
15
17
using Microsoft . AspNet . HttpFeature ;
18
+ using Microsoft . AspNet . HttpFeature . Security ;
16
19
17
20
namespace Microsoft . AspNet . Owin
18
21
{
@@ -28,12 +31,14 @@ public OwinEnvironment(HttpContext context)
28
31
_context = context ;
29
32
_entries = new Dictionary < string , FeatureMap > ( )
30
33
{
34
+ { OwinConstants . CallCancelled , new FeatureMap < IHttpRequestLifetimeFeature > ( feature => feature . OnRequestAborted ) } ,
31
35
{ OwinConstants . RequestProtocol , new FeatureMap < IHttpRequestFeature > ( feature => feature . Protocol , ( feature , value ) => feature . Protocol = Convert . ToString ( value ) ) } ,
32
36
{ OwinConstants . RequestScheme , new FeatureMap < IHttpRequestFeature > ( feature => feature . Scheme , ( feature , value ) => feature . Scheme = Convert . ToString ( value ) ) } ,
33
37
{ OwinConstants . RequestMethod , new FeatureMap < IHttpRequestFeature > ( feature => feature . Method , ( feature , value ) => feature . Method = Convert . ToString ( value ) ) } ,
34
38
{ OwinConstants . RequestPathBase , new FeatureMap < IHttpRequestFeature > ( feature => feature . PathBase , ( feature , value ) => feature . PathBase = Convert . ToString ( value ) ) } ,
35
39
{ OwinConstants . RequestPath , new FeatureMap < IHttpRequestFeature > ( feature => feature . Path , ( feature , value ) => feature . Path = Convert . ToString ( value ) ) } ,
36
- { OwinConstants . RequestQueryString , new FeatureMap < IHttpRequestFeature > ( feature => feature . QueryString , ( feature , value ) => feature . QueryString = Convert . ToString ( value ) ) } ,
40
+ { OwinConstants . RequestQueryString , new FeatureMap < IHttpRequestFeature > ( feature => RemoveQuestionMark ( feature . QueryString ) ,
41
+ ( feature , value ) => feature . QueryString = AddQuestionMark ( Convert . ToString ( value ) ) ) } ,
37
42
{ OwinConstants . RequestHeaders , new FeatureMap < IHttpRequestFeature > ( feature => feature . Headers , ( feature , value ) => feature . Headers = ( IDictionary < string , string [ ] > ) value ) } ,
38
43
{ OwinConstants . RequestBody , new FeatureMap < IHttpRequestFeature > ( feature => feature . Body , ( feature , value ) => feature . Body = ( Stream ) value ) } ,
39
44
@@ -56,6 +61,8 @@ public OwinEnvironment(HttpContext context)
56
61
{ OwinConstants . CommonKeys . IsLocal , new FeatureMap < IHttpConnectionFeature > ( feature => feature . IsLocal , ( feature , value ) => feature . IsLocal = Convert . ToBoolean ( value ) ) } ,
57
62
58
63
{ OwinConstants . SendFiles . SendAsync , new FeatureMap < IHttpSendFileFeature > ( feature => new SendFileFunc ( feature . SendFileAsync ) ) } ,
64
+
65
+ { OwinConstants . Security . User , new FeatureMap < IHttpAuthenticationFeature > ( feature => feature . User , ( feature , value ) => feature . User = MakeClaimsPrincipal ( ( IPrincipal ) value ) ) } ,
59
66
} ;
60
67
61
68
if ( context . Request . IsSecure )
@@ -222,6 +229,36 @@ IEnumerator IEnumerable.GetEnumerator()
222
229
throw new NotImplementedException ( ) ;
223
230
}
224
231
232
+ private string RemoveQuestionMark ( string queryString )
233
+ {
234
+ if ( ! string . IsNullOrEmpty ( queryString ) )
235
+ {
236
+ if ( queryString [ 0 ] == '?' )
237
+ {
238
+ return queryString . Substring ( 1 ) ;
239
+ }
240
+ }
241
+ return queryString ;
242
+ }
243
+
244
+ private string AddQuestionMark ( string queryString )
245
+ {
246
+ if ( ! string . IsNullOrEmpty ( queryString ) )
247
+ {
248
+ return '?' + queryString ;
249
+ }
250
+ return queryString ;
251
+ }
252
+
253
+ private ClaimsPrincipal MakeClaimsPrincipal ( IPrincipal principal )
254
+ {
255
+ if ( principal is ClaimsPrincipal )
256
+ {
257
+ return principal as ClaimsPrincipal ;
258
+ }
259
+ return new ClaimsPrincipal ( principal ) ;
260
+ }
261
+
225
262
public class FeatureMap
226
263
{
227
264
public FeatureMap ( Type featureInterface , Func < object , object > getter )
0 commit comments