1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Reflection ;
4+ using System . Security . Claims ;
5+ using System . Threading . Tasks ;
6+ using System . Web . Http ;
7+ using Autofac ;
8+ using Autofac . Core ;
9+ using Autofac . Integration . Owin ;
10+ using Autofac . Integration . WebApi ;
11+ using Microsoft . Owin ;
12+ using Microsoft . Owin . Logging ;
13+ using Microsoft . Owin . Security . Authorization ;
14+ using Microsoft . Owin . Security . Authorization . Infrastructure ;
15+ using Owin ;
16+ using WebApi_Autofac ;
17+ using WebApi_Autofac . Models ;
18+
19+ [ assembly: OwinStartup ( typeof ( Startup ) ) ]
20+
21+ namespace WebApi_Autofac
22+ {
23+ public class Startup
24+ {
25+ public delegate AuthorizationDependencies AuthorizationDependenciesFactory ( AuthorizationOptions options ) ;
26+
27+ public void Configuration ( IAppBuilder app )
28+ {
29+ app . UseErrorPage ( ) ;
30+ app . Use ( AddEmployeeClaimBeforeAuthorizationCheck ) ;
31+
32+ var builder = new ContainerBuilder ( ) ;
33+
34+ var config = new HttpConfiguration ( ) ;
35+ WebApiConfig . Register ( config ) ;
36+
37+ builder . RegisterApiControllers ( Assembly . GetExecutingAssembly ( ) ) ;
38+ builder . RegisterType < CustomAuthorizationPolicyProvider > ( ) . As < IAuthorizationPolicyProvider > ( ) . InstancePerRequest ( ) ;
39+ builder . RegisterAssemblyTypes ( Assembly . GetExecutingAssembly ( ) ) . Where ( t => typeof ( IAuthorizationHandler ) . IsAssignableFrom ( t ) ) . InstancePerRequest ( ) . AsImplementedInterfaces ( ) ;
40+ builder . RegisterType < DefaultAuthorizationService > ( ) . As < IAuthorizationService > ( ) . InstancePerRequest ( ) ;
41+ builder . RegisterType < AuthorizationDependencies > ( ) . InstancePerRequest ( ) . PropertiesAutowired ( ) ;
42+ builder . RegisterInstance ( new DiagnosticsLoggerFactory ( ) . Create ( "WebApi_Autofac_Logger" ) )
43+ . As < ILogger > ( )
44+ . SingleInstance ( ) ;
45+
46+ var container = builder . Build ( ) ;
47+ config . DependencyResolver = new AutofacWebApiDependencyResolver ( container ) ;
48+
49+ app . UseAutofacMiddleware ( container ) ;
50+ app . UseAutofacWebApi ( config ) ;
51+
52+ app . UseAuthorization ( options =>
53+ {
54+ options . AddPolicy ( ExampleConstants . EmployeeNumber2Policy , policyBuilder =>
55+ {
56+ policyBuilder . AddRequirements ( new EmployeeNumber2Requirement ( ) ) ;
57+ } ) ;
58+ } , new AuthorizationDependenciesProvider
59+ (
60+ ( options , context ) =>
61+ {
62+ var optionsParameter = new ResolvedParameter (
63+ ( pi , ctx ) => pi . ParameterType == typeof ( AuthorizationOptions ) ,
64+ ( pi , ctx ) => options ) ;
65+
66+ context . GetAutofacLifetimeScope ( ) . Resolve < IAuthorizationPolicyProvider > ( optionsParameter ) ;
67+ var dependenciesFactory = context . GetAutofacLifetimeScope ( ) . Resolve < Func < AuthorizationOptions , AuthorizationDependencies > > ( ) ;
68+ var dependencies = dependenciesFactory ? . Invoke ( options ) ;
69+ return dependencies ;
70+ }
71+ ) ) ;
72+
73+ app . UseWebApi ( config ) ;
74+ }
75+
76+ private static async Task AddEmployeeClaimBeforeAuthorizationCheck ( IOwinContext owinContext , Func < Task > next )
77+ {
78+ var currentIdentity = ( ClaimsIdentity ) owinContext . Authentication . User . Identity ;
79+ if ( ! currentIdentity . HasClaim ( x => x . Type == ExampleConstants . EmployeeClaimType ) )
80+ {
81+ const string currentEmployeeNumber = "2" ;
82+ currentIdentity . AddClaim ( new Claim ( ExampleConstants . EmployeeClaimType , currentEmployeeNumber ) ) ;
83+ currentIdentity . AddClaim ( new Claim ( "IsUser" , "true" ) ) ;
84+ currentIdentity . AddClaim ( new Claim ( "IsAdmin" , "false" ) ) ;
85+ }
86+ await next ( ) ;
87+ }
88+ }
89+ }
0 commit comments