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