88using System . IO ;
99using System . Linq ;
1010using System . Net ;
11+ using System . Security . Claims ;
1112using System . Security . Cryptography . X509Certificates ;
13+ using System . Security . Principal ;
1214using System . Threading ;
1315using System . Threading . Tasks ;
1416using Microsoft . AspNet . Http ;
1517using Microsoft . AspNet . HttpFeature ;
18+ using Microsoft . AspNet . HttpFeature . Security ;
1619
1720namespace Microsoft . AspNet . Owin
1821{
@@ -28,12 +31,14 @@ public OwinEnvironment(HttpContext context)
2831 _context = context ;
2932 _entries = new Dictionary < string , FeatureMap > ( )
3033 {
34+ { OwinConstants . CallCancelled , new FeatureMap < IHttpRequestLifetimeFeature > ( feature => feature . OnRequestAborted ) } ,
3135 { OwinConstants . RequestProtocol , new FeatureMap < IHttpRequestFeature > ( feature => feature . Protocol , ( feature , value ) => feature . Protocol = Convert . ToString ( value ) ) } ,
3236 { OwinConstants . RequestScheme , new FeatureMap < IHttpRequestFeature > ( feature => feature . Scheme , ( feature , value ) => feature . Scheme = Convert . ToString ( value ) ) } ,
3337 { OwinConstants . RequestMethod , new FeatureMap < IHttpRequestFeature > ( feature => feature . Method , ( feature , value ) => feature . Method = Convert . ToString ( value ) ) } ,
3438 { OwinConstants . RequestPathBase , new FeatureMap < IHttpRequestFeature > ( feature => feature . PathBase , ( feature , value ) => feature . PathBase = Convert . ToString ( value ) ) } ,
3539 { 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 ) ) ) } ,
3742 { OwinConstants . RequestHeaders , new FeatureMap < IHttpRequestFeature > ( feature => feature . Headers , ( feature , value ) => feature . Headers = ( IDictionary < string , string [ ] > ) value ) } ,
3843 { OwinConstants . RequestBody , new FeatureMap < IHttpRequestFeature > ( feature => feature . Body , ( feature , value ) => feature . Body = ( Stream ) value ) } ,
3944
@@ -56,6 +61,8 @@ public OwinEnvironment(HttpContext context)
5661 { OwinConstants . CommonKeys . IsLocal , new FeatureMap < IHttpConnectionFeature > ( feature => feature . IsLocal , ( feature , value ) => feature . IsLocal = Convert . ToBoolean ( value ) ) } ,
5762
5863 { 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 ) ) } ,
5966 } ;
6067
6168 if ( context . Request . IsSecure )
@@ -222,6 +229,36 @@ IEnumerator IEnumerable.GetEnumerator()
222229 throw new NotImplementedException ( ) ;
223230 }
224231
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+
225262 public class FeatureMap
226263 {
227264 public FeatureMap ( Type featureInterface , Func < object , object > getter )
0 commit comments