2121
2222namespace TodoListWebApp
2323{
24- public class Startup
24+ public partial class Startup
2525 {
26- public static string Authority = String . Empty ;
27- public static string ClientId = String . Empty ;
28- public static string AppKey = String . Empty ;
29- public static string TodoListResourceId = String . Empty ;
30- public static string TodoListBaseAddress = String . Empty ;
31-
3226 public Startup ( IHostingEnvironment env )
3327 {
3428 // Setup configuration sources.
3529 Configuration = new Configuration ( )
3630 . AddJsonFile ( "config.json" )
3731 . AddEnvironmentVariables ( ) ;
38-
39- Authority = String . Format ( Configuration . Get ( "AzureAd:AadInstance" ) , Configuration . Get ( "AzureAd:Tenant" ) ) ;
40- ClientId = Configuration . Get ( "AzureAd:ClientId" ) ;
41- AppKey = Configuration . Get ( "AzureAd:AppKey" ) ;
42- TodoListResourceId = Configuration . Get ( "AzureAd:TodoListResourceId" ) ;
43- TodoListBaseAddress = Configuration . Get ( "AzureAd:TodoListBaseAddress" ) ;
4432 }
4533
4634 public IConfiguration Configuration { get ; set ; }
@@ -50,14 +38,16 @@ public void ConfigureServices(IServiceCollection services)
5038 {
5139 // Add MVC services to the services container.
5240 services . AddMvc ( ) ;
41+
42+ // Add Session Middleware
5343 services . AddCachingServices ( ) ;
5444 services . AddSessionServices ( ) ;
5545
46+ // Add Cookie Middleware
5647 services . Configure < ExternalAuthenticationOptions > ( options =>
5748 {
5849 options . SignInAsAuthenticationType = CookieAuthenticationDefaults . AuthenticationType ;
5950 } ) ;
60-
6151 }
6252
6353 // Configure is called after ConfigureServices is called.
@@ -67,7 +57,6 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
6757 // Add the console logger.
6858 loggerfactory . AddConsole ( ) ;
6959
70-
7160 // Add the following to the request pipeline only in development environment.
7261 if ( string . Equals ( env . EnvironmentName , "Development" , StringComparison . OrdinalIgnoreCase ) )
7362 {
@@ -81,26 +70,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
8170 app . UseErrorHandler ( "/Home/Error" ) ;
8271 }
8372
84- // Add static files to the request pipeline.
85- app . UseStaticFiles ( ) ;
86-
87- // Configure the app with session middleware, which we will use to store tokens.
88- app . UseSession ( ) ;
89-
90- // Configure the OWIN Pipeline to use OpenID Connect Authentication
91- app . UseCookieAuthentication ( options => { } ) ;
92-
93- app . UseOpenIdConnectAuthentication ( options =>
94- {
95- options . ClientId = Configuration . Get ( "AzureAd:ClientId" ) ;
96- options . Authority = Authority ;
97- options . PostLogoutRedirectUri = Configuration . Get ( "AzureAd:PostLogoutRedirectUri" ) ;
98- options . RedirectUri = Configuration . Get ( "AzureAd:PostLogoutRedirectUri" ) ;
99- options . Notifications = new OpenIdConnectAuthenticationNotifications
100- {
101- AuthorizationCodeReceived = OnAuthorizationCodeReceived
102- } ;
103- } ) ;
73+ // Configure the OpenIdConnect Auth Pipeline and required services.
74+ ConfigureAuth ( app ) ;
10475
10576 // Add MVC to the request pipeline.
10677 app . UseMvc ( routes =>
@@ -114,15 +85,5 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
11485 // routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
11586 } ) ;
11687 }
117-
118- public async Task OnAuthorizationCodeReceived ( AuthorizationCodeReceivedNotification notification )
119- {
120- string userObjectId = notification . AuthenticationTicket . Principal . FindFirst ( "http://schemas.microsoft.com/identity/claims/objectidentifier" ) . Value ;
121- ClientCredential clientCred = new ClientCredential ( ClientId , AppKey ) ;
122- AuthenticationContext authContext = new AuthenticationContext ( Authority , new NaiveSessionCache ( userObjectId , notification . HttpContext . Session ) ) ;
123- AuthenticationResult authResult = await authContext . AcquireTokenByAuthorizationCodeAsync (
124- notification . Code , new Uri ( notification . RedirectUri ) , clientCred , Startup . TodoListResourceId ) ;
125-
126- }
12788 }
12889}
0 commit comments