Home
%3CLINGO-SUB%20id%3D%22lingo-sub-775331%22%20slang%3D%22en-US%22%3EPutting%20it%20all%20together%20%E2%80%93%20CORS%20tutorial%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-775331%22%20slang%3D%22en-US%22%3E%3CP%3EHosting%20the%20two%20sites%20on%20IIS%20and%20understanding%20the%20request%20flow%20and%20needed%20configuration%20to%20allow%20the%20entire%20application%20work%20correctly%20is%20what%20we%20will%20focus%20on%20in%20this%20article.%20This%20far%2C%20in%20this%20tutorial%20we%20have%20gone%20through%20creating%20the%20front-end%20website%20using%20Razor%20Pages%20and%20the%20back-end%20HTTP%20web-service%20using%20WebAPI%2C%20as%20well%20as%20preparing%20the%20two%20solutions%20for%20publishing%20on%20IIS%20as%20explained%20in%3A%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Faspnet%2Fcore%2Fpublishing%2Fiis%3Ftabs%3Daspnetcore2x%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Faspnet%2Fcore%2Fpublishing%2Fiis%3Ftabs%3Daspnetcore2x%20%3C%2FA%3E%3C%2FSPAN%3E.%20Since%20the%20application%20is%20expected%20to%20run%20using%20Windows%20Integrated%20authentication%20there%20will%20be%20a%20couple%20of%20more%20steps%20that%20we%20will%20need%20to%20take%20in%20both%20solutions%20to%20make%20sure%20that%20the%20ASP.net%20Core%20interacts%20correctly%20with%20IIS%20and%20is%20forwarded%20the%20authentication%20tokens%20from%20the%20IIS%20worker%20process%20(w3wp.exe).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20first%20thing%20to%20accomplish%2C%20is%20to%20setup%20the%20applications%20to%20work%20with%20IIS.%20This%20is%20achieved%20in%20the%20Startup.cs%20class%20of%20each%20of%20the%20two%20projects.%20We%20will%20be%20adding%20IIS%20as%20a%20service%2C%20in%20the%20ConfigureServices%20method%2C%20using%20the%20options%20pattern.%20The%20only%20option%20we%20are%20interested%20in%20is%20having%20the%20applications%20configured%20for%20Windows%20Authentication.%20This%20is%20done%20by%20setting%20the%20AutomaticAuthentication%20option%20and%20setting%20to%20true.%20The%20code%20is%20shown%20below%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Epublic%20void%20ConfigureServices(IServiceCollection%20services)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20services.AddMvc()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fneeded%20to%20enable%20IIS%20integration%20with%20Windows%20Auth%0A%20%20%20%20%20%20%20%20%20%20%20%20services.Configure%3CIISOPTIONS%3E(options%20%3D%26gt%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20options.AutomaticAuthentication%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20%20%20%7D%3C%2FIISOPTIONS%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20highlighted%20code%20is%20what%20needs%20to%20be%20added%20to%20the%20ConfigureServices%20method%20in%20both%20projects%2C%20after%20which%20they%20should%20be%20recompiled%20and%20published.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EInside%20the%20IIS%20Manager%20Console%2C%20I%20recommend%20setting%20two%20websites%20(they%20can%20be%20on%20different%20servers%20or%20on%20the%20same%20server)%20and%20setting%20up%20HTTP%20bindings%20with%20host%20header%20entries%2C%20for%20%3CSTRONG%3Ecorerazor%3C%2FSTRONG%3Eand%20%3CSTRONG%3Ecorewebapi%3C%2FSTRONG%3E%2C%20by%20selecting%20the%20%E2%80%98Bindings%E2%80%99%20action%20for%20each%20website%3A%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F124624iB27D4F6AC1FE9631%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHost%20bindings%20for%20CoreRazor%20%E2%80%93%20front%20end%20website%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20388px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F124625i33122A4D32736812%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHost%20bindings%20for%20CoreWebAPI%20%E2%80%93%20backend%20http%20webservice%20application%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20390px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F124626i493EB641AC932E20%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3ESIDE%20NOTE%3A%3C%2FEM%3EIf%20you%20plan%20to%20host%20both%20websites%20on%20the%20same%20Windows%20server%2C%20and%20if%20you%20have%20no%20DNS%20so%20that%20you%20can%20setup%20CNAME%20aliases%20for%20the%20hostnames%20%3CSTRONG%3EcoreRazor%3C%2FSTRONG%3Eand%20%3CSTRONG%3EcoreWebApi%3C%2FSTRONG%3E%2C%20you%20can%20modify%20the%20hosts%20file%20of%20the%20computer%20you%20will%20be%20accessing%20the%20website%20from%20to%20map%20the%20two%20entries%20to%20your%20server%E2%80%99s%20IP%20address.%20To%20do%20this%2C%20you%20may%20wish%20to%20follow%20the%20article%20below%3A%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.petri.com%2Feasily-edit-hosts-file-windows-10%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.petri.com%2Feasily-edit-hosts-file-windows-10%3C%2FA%3E%3C%2FSPAN%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20second%20step%20in%20configuring%20the%20two%20applications%20to%20use%20Windows%20Integrated%20authentication%2C%20is%20to%20configure%20the%20ASP.net%20Core%20Platforms%20Handler%20to%20forward%20the%20Windows%20integrated%20authentication%20token%20to%20the%20dotnet.exe%20process%20that%20is%20hosting%20the%20application.%20The%20easiest%20way%20to%20achieve%20this%20is%20using%20the%20Configuration%20Editor%20in%20the%20IIS%20Mangeger%20console.%20Just%20select%20each%20of%20your%20sites%2C%20and%20then%20double%20click%20the%20%E2%80%98Configuration%20Editor%E2%80%99%20icon%20in%20the%20middle%20pane%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F124627i681979466D512242%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20inside%20this%20feature%2C%20you%20will%20be%20able%20to%20access%20any%20configuration%20relating%20to%20the%20website%2C%20that%20is%20coming%20either%20from%20ApplicationHost.config%20file%20or%20the%20website%E2%80%99s%20web.config%20file.%20Using%20the%20dropdown%20control%20from%20the%20top%20of%20the%20Configuration%20Editor%20window%2C%20select%20the%20System.WebServer%2FAspNetCore%20tag%20from%20the%20list%20of%20available%20tags%20as%20shown%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F124628i4E8C2BBA7947AF28%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWithin%20this%20section%2C%20you%20will%20find%20all%20of%20the%20possible%20configuration%20for%20the%20ASP.net%20Core%20Platforms%20Handler.%20Among%20the%20settings%20is%20one%20called%20%3CSTRONG%3EforwardWindowsAuthToken%3C%2FSTRONG%3Ewhish%20should%20be%20set%20to%20%E2%80%98true%E2%80%99%2C%20in%20order%20for%20the%20module%20to%20forward%20the%20authentication%20token%20to%20the%20dotnet.exe%20process.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%2C%20we%20need%20to%20understand%20the%20request%20flow%2C%20which%20may%20be%20the%20most%20important%20part%20of%20this%20tutorial.%20Nothing%20gets%20the%20point%20across%20better%20than%20a%20visual%20diagram%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F124629i89AC3C746E304F0F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20above%20diagram%2C%20the%20client%20browser%20will%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EMake%20a%20request%20to%20%3CU%3Ehttp%3A%2F%2FcoreRazor%2FasyncPoster%3C%2FU%3E.%20The%20request%20will%20be%20of%20type%20GET%20and%20will%20result%20in%20the%20Razor%20Page%20executing%20on%20the%20server%20and%20sending%20back%20some%20HTML%20markup%20to%20the%20browser%20to%20render.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20the%20page%20is%20displayed%2C%20the%20user%20will%20proceed%20and%20enter%20some%20text%20in%20the%20text%20area%20control%20and%20press%20the%20%E2%80%98Echo%20Text%E2%80%99%20button.%20At%20this%20point%20the%20interesting%20stuff%20starts%20to%20happen.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%222%22%3E%0A%3CLI%3EThe%20JavaScript%20engine%20will%20send%20a%20CORS%20preflight%20request%20to%20the%20%3CSPAN%3E%3CA%20href%3D%22http%3A%2F%2FcoreWebAPI%2Fapi%2FEchoCors%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2FcoreWebAPI%2Fapi%2FEchoCors%3C%2FA%3E%3C%2FSPAN%3Eurl.%20The%20purpose%20of%20this%20request%20is%20to%20make%20sure%20that%20the%20website%20allows%20requests%20coming%20from%20another%20site%20(notably%20%3CU%3Ehttp%3A%2F%2FcoreRazor%3C%2FU%3E)%20to%20be%20sent%20and%20processed.%20The%20request%20materializes%20as%20an%20OPTIONS%20request%20on%20the%20wire%2C%20and%20the%20following%20HTTP%20headers%20are%20sent%20out%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EOPTIONS%20%2Fapi%2FEchoCors%20HTTP%2F1.1%0AHost%3A%20corewebapi%0AConnection%3A%20keep-alive%0AAccess-Control-Request-Method%3A%20POST%0AOrigin%3A%20http%3A%2F%2Fcorerazor%0AUser-Agent%3A%20Mozilla%2F5.0%20(Windows%20NT%2010.0%3B%20Win64%3B%20x64)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F62.0.3202.94%20Safari%2F537.36%0AAccess-Control-Request-Headers%3A%20content-type%20Accept%3A%20*%2F*%0AAccept-Encoding%3A%20gzip%2C%20deflate%20%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20have%20highlighted%20the%20important%20information%20in%20gray%20%E2%80%93%20the%20Origin%20header%20indicates%20where%20the%20Javascript%20that%20is%20performing%20the%20CORS%20request%20originated%20from%3A%20%3CU%3Ehttp%3A%2F%2Fcorerazor%3C%2FU%3E.%20The%20Access-Control-Request-Method%20indicates%20what%20kind%20of%20request%20the%20script%20would%20like%20to%20make%20(a%20POST%20request%20in%20this%20case).%20According%20to%3A%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Ffetch.spec.whatwg.org%2F%23cors-preflight-fetch%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Ffetch.spec.whatwg.org%2F%23cors-preflight-fetch%3C%2FA%3E%3C%2FSPAN%3E%2C%20the%20CORS%20preflight%20request%20has%20to%20go%20out%20without%20any%20authentication.%20And%20this%20is%20the%20main%20problem%20in%20our%20case.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20have%20followed%20along%20with%20the%20tutorial%20up%20to%20this%20point%2C%20you%20will%20know%20that%20Windows%20Integrated%20Authentication%20will%20reject%20any%20requests%20that%20are%20sent%20without%20credentials%2C%20and%20the%20response%20from%20the%20IIS%20server%20will%20be%20a%20401%20status%20code%2C%20requesting%20authentication.%20This%20will%20not%20allow%20the%20JavaScript%20engine%20to%20know%20if%20requests%20from%20the%20origin%20site%20are%20allowed%20to%20the%20target%20backend%20site%20and%20will%20result%20in%20failure.%3C%2FP%3E%0A%3CP%3EIn%20order%20to%20remedy%20this%2C%20we%20need%20to%20modify%20the%20IIS%20settings%2C%20for%20the%20backend%20%3CU%3Ehttp%3A%2F%2FcoreWebAPI%3C%2FU%3Esite%20to%20allow%20anonymous%20requests%20in%20certain%20cases.%20This%20is%20where%20a%20good%20understanding%20of%20the%20HTTP%20pipeline%20in%20IIS%20comes%20in%20handy%2C%20and%20I%20would%20suggest%20a%20detour%20to%20video%20number%202%20in%20the%20IIS%20Architecture%20and%20Components%20videos%20I%20have%20posted%20online%20a%20while%20ago%3A%20%3CSPAN%3E%3CA%20href%3D%22http%3A%2F%2Flinqto.me%2Fn%2FIISArchitecture%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Flinqto.me%2Fn%2FIISArchitecture%3C%2FA%3E%3C%2FSPAN%3E.%20The%20HTTP%20processing%20pipeline%20is%20based%20on%20events%20being%20raised%20and%20pieces%20of%20code%20called%20%E2%80%98http%20modules%E2%80%99%20subscribing%20to%20the%20notifications%20sent%20out%20on%20these%20events%20to%20perform%20changes%20to%20the%20request%20or%20the%20response.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETwo%20pipeline%20stages%20are%20important%20here%3A%201)%20Authentication%20and%202)%20Authorization.%20In%20the%20authentication%20stage%2C%20we%20are%20trying%20to%20understand%20who%20the%20user%20behind%20the%20request%20is.%20So%2C%20we%20may%20choose%20to%20say%20the%20user%20is%20the%20%E2%80%98anonymous%20user%E2%80%99%2C%20and%20that%20anonymous%20authentication%20should%20also%20be%20turned%20on.%20Below%20are%20the%20authentication%20settings%20on%20IIS%20for%20the%20%3CU%3Ehttp%3A%2F%2FcoreWebAPI%20%3C%2FU%3Esite%20%E2%80%93%20note%20that%20both%20Windows%20Integrated%20Authentication%20and%20Anonymous%20Authentication%20are%20enabled%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F124630iC2924EE11143A342%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20next%20stage%20of%20request%20processing%2C%20the%20Authorization%20stage%20(or%20Authorization%20event)%2C%20we%20need%20to%20make%20sure%20we%20only%20allow%20the%20anonymous%20requests%20from%20CORS%20preflight%20and%20require%20all%20other%20incoming%20requests%20have%20authentication%20credentials%20supplied.%20We%20can%20achieve%20this%20through%20Authorization%20Rules.%20A%20default%20authorization%20rule%20granting%20all%20users%20access%20to%20the%20site%20is%20already%20in%20place%20and%20supplied%20by%20default%20by%20IIS.%20We%20will%20start%20by%20modifying%20this%20rule%20to%20only%20allow%20anonymous%20users%2C%20if%20they%20send%20requests%20that%20are%20using%20the%20OPTIONS%20http%20verb.%20Below%20is%20the%20target%20configuration%20in%20IIS%20for%20this%20authorization%20rule%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20477px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F124631i51DC64EDCA530574%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHowever%2C%20this%20authorization%20rule%20is%20not%20enough%20as%20it%20will%20block%20all%20other%20requests.%20We%20need%20a%20second%20%E2%80%98Allow%E2%80%99%20Authorization%20rule%20that%20needs%20to%20be%20introduced.%20This%20rule%20will%20permit%20access%20to%20the%20application%20to%20a%20desired%20group%20of%20users%20%E2%80%93%20for%20the%20demonstration%2C%20I%20have%20chosen%20the%20%E2%80%98Users%E2%80%99%20group%2C%20which%20should%20include%20most%20authenticated%20user%20accounts%20from%20the%20server%20where%20the%20backend%20application%20is%20hosted.%20In%20a%20real-world%20scenario%2C%20we%20would%20configure%20an%20Active%20Directory%20group%2C%20such%20as%20%E2%80%98Domain%20Users%E2%80%99%20if%20a%20really%20large%20scope%20was%20targeted%2C%20or%20some%20more%20restrained%2C%20like%20a%20specific%20user%20group.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20379px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F124633i313B50E403205A23%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20what%20the%20web.config%20file%20for%20the%20backend%20website%20will%20contain%20following%20the%20modifications%20to%20the%20authorization%20rules%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%3CSYSTEM.WEBSERVER%3E%0A%20%20%20%20%3CHANDLERS%3E%0A%20%20%20%20%20%20%3CADD%20name%3D%22aspNetCore%22%20path%3D%22*%22%20verb%3D%22*%22%20modules%3D%22AspNetCoreModule%22%20resourcetype%3D%22Unspecified%22%3E%3C%2FADD%3E%0A%20%20%20%20%3C%2FHANDLERS%3E%0A%0A%20%20%20%20%3CASPNETCORE%20processpath%3D%22dotnet%22%20arguments%3D%22.%5CWebApiEcho.dll%22%20stdoutlogenabled%3D%22false%22%20stdoutlogfile%3D%22.%5Clogs%5Cstdout%22%3E%3C%2FASPNETCORE%3E%0A%20%20%20%20%20%20%20%20%3CSECURITY%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CAUTHENTICATION%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CANONYMOUSAUTHENTICATION%20enabled%3D%22true%22%3E%3C%2FANONYMOUSAUTHENTICATION%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CWINDOWSAUTHENTICATION%20enabled%3D%22true%22%3E%3C%2FWINDOWSAUTHENTICATION%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2FAUTHENTICATION%3E%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CAUTHORIZATION%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CREMOVE%20users%3D%22*%22%20roles%3D%22%22%20verbs%3D%22%22%3E%3C%2FREMOVE%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CADD%20accesstype%3D%22Allow%22%20users%3D%22%3F%22%20verbs%3D%22OPTIONS%22%3E%3C%2FADD%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3CADD%20accesstype%3D%22Allow%22%20users%3D%22%22%20roles%3D%22USERS%22%3E%3C%2FADD%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2FAUTHORIZATION%3E%0A%20%20%20%20%20%20%20%20%3C%2FSECURITY%3E%0A%20%20%3C%2FSYSTEM.WEBSERVER%3E%0A%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThese%20two%20rules%20will%20ensure%20that%3A%20CORS%20preflight%20OPTION%20request%20rules%20will%20be%20honored%20without%20requiring%20authentication%2C%20while%20all%20other%20incoming%20requests%20will%20need%20to%20present%20valid%20credentials%20to%20be%20allowed%20through%20to%20our%20ASP.net%20Core%20WebAPI%20application.%3C%2FP%3E%0A%3CP%3EIf%20we%20review%20the%20response%20headers%20for%20the%20CORS%20preflight%20request%20shown%20earlier%2C%20we%20can%20see%20that%20the%20server%20does%20authorize%20the%20receipt%20of%20CORS%20request%20from%20the%20%3CU%3Ehttp%3A%2F%2FcoreRazor%3C%2FU%3Ewebsite%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EHTTP%2F1.1%20200%20OK%0AVary%3A%20Origin%0AServer%3A%20Kestrel%0AAccess-Control-Allow-Credentials%3A%20true%0AAccess-Control-Allow-Headers%3A%20content-type%0AAccess-Control-Allow-Origin%3A%20http%3A%2F%2Fcorerazor%0AX-Powered-By%3A%20ASP.NET%0AContent-Length%3A%200%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20highlighted%20headers%2C%20we%20can%20see%20that%20the%20server%20is%20indicating%20that%20CORS%20requests%20to%20this%20server%20from%20the%20website%20are%20allowed%20if%20they%20supply%20credentials%2C%20and%20if%20the%20requests%20originate%20from%20the%20%3CU%3Ehttp%3A%2F%2FcoreRazor%20%3C%2FU%3Esite.%20With%20this%20information%2C%20the%20JavaScript%20engine%20can%20now%20proceed%20to%20perform%20the%20POST%20request%20with%20JSON%20content%20to%20send%20to%20the%20WebAPI%20controller%20for%20processing.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23800000%22%3E%3CSTRONG%3EPost%20Scriptum%3A%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EFollowing%20the%20writing%20of%20this%20article%2C%20a%20new%20module%20called%20the%20IIS%20CORS%20module%20has%20become%20available.%20The%20module%20can%20be%20downloaded%20from%20the%20following%20link%3A%20%3CA%20href%3D%22https%3A%2F%2Fwww.iis.net%2Fdownloads%2Fmicrosoft%2Fiis-cors-module%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.iis.net%2Fdownloads%2Fmicrosoft%2Fiis-cors-module%3C%2FA%3E.%20Documentation%20on%20the%20module%20is%20available%20below%2C%20as%20well%20as%20configuration%20settings%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fiis%2Fextensions%2Fcors-module%2Fcors-module-configuration-reference%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fiis%2Fextensions%2Fcors-module%2Fcors-module-configuration-reference%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20way%20in%20which%20the%20module%20works%20is%20by%20hooking%20into%20the%20IIS%20pipeline%20very%20early%20on%20and%20intercepting%20and%20inspecting%20OPTIONS%20requests.%20If%20the%20request%20is%20determined%20to%20be%20a%20CORS%20preflight%20request%2C%20the%20request%20is%20handled%20even%20before%20the%20authentication%20stage%20of%20the%20IIS%20pipeline%20can%20be%20reached.%20This%20allows%20the%20module%20to%20handle%20requests%20even%20on%20websites%20that%20do%20not%20allow%20Anonymous%20authentication%20and%20then%20setup%20the%20authorization%20rules%20as%20described%20above.%20All%20you%20have%20to%20do%20is%20to%20install%20and%20configure%20the%20module%2C%20bypassing%20the%20configuration%20of%20the%20authorization%20rules.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWritten%20by%3A%20%3CA%20href%3D%22http%3A%2F%2Flinqto.me%2Fabout%2Fpcociuba%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPaul%20Cociuba%3C%2FA%3E%3CBR%20%2F%3EReviewed%20by%3A%20Muna%20AlHasan%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-775331%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20is%20a%20tutorial%20on%20implementing%20ASP.net%20Core%202%20Razor%20Pages%20App%20that%20uses%20CORS%20requests.%3C%2FP%3E%0A%3CP%3EIn%20this%20part%20we%20will%20be%20looking%20at%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3Ehow%20to%20configure%20both%20of%20these%20sites%20to%20work%20with%20IIS%20as%20a%20reverse%20proxy%20server%2C%20and%20implement%20Windows%20Integrated%20Authentication%2C%20as%20would%20be%20the%20case%20for%20many%20such%20setups%20running%20in%20corporate%20intranets.%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-775331%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Epaulboc%40microsoft.com%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Hosting the two sites on IIS and understanding the request flow and needed configuration to allow the entire application work correctly is what we will focus on in this article. This far, in this tutorial we have gone through creating the front-end website using Razor Pages and the back-end HTTP web-service using WebAPI, as well as preparing the two solutions for publishing on IIS as explained in: https://docs.microsoft.com/en-us/aspnet/core/publishing/iis?tabs=aspnetcore2x . Since the application is expected to run using Windows Integrated authentication there will be a couple of more steps that we will need to take in both solutions to make sure that the ASP.net Core interacts correctly with IIS and is forwarded the authentication tokens from the IIS worker process (w3wp.exe).

 

The first thing to accomplish, is to setup the applications to work with IIS. This is achieved in the Startup.cs class of each of the two projects. We will be adding IIS as a service, in the ConfigureServices method, using the options pattern. The only option we are interested in is having the applications configured for Windows Authentication. This is done by setting the AutomaticAuthentication option and setting to true. The code is shown below:

 

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            //needed to enable IIS integration with Windows Auth
            services.Configure<IISOptions>(options =>
            {
                options.AutomaticAuthentication = true;
            });
        }

 

The highlighted code is what needs to be added to the ConfigureServices method in both projects, after which they should be recompiled and published.

 

Inside the IIS Manager Console, I recommend setting two websites (they can be on different servers or on the same server) and setting up HTTP bindings with host header entries, for corerazor and corewebapi, by selecting the ‘Bindings’ action for each website:


image.png

 

Host bindings for CoreRazor – front end website

image.png

 

Host bindings for CoreWebAPI – backend http webservice application

image.png

 

SIDE NOTE: If you plan to host both websites on the same Windows server, and if you have no DNS so that you can setup CNAME aliases for the hostnames coreRazor and coreWebApi , you can modify the hosts file of the computer you will be accessing the website from to map the two entries to your server’s IP address. To do this, you may wish to follow the article below: https://www.petri.com/easily-edit-hosts-file-windows-10 .

 

The second step in configuring the two applications to use Windows Integrated authentication, is to configure the ASP.net Core Platforms Handler to forward the Windows integrated authentication token to the dotnet.exe process that is hosting the application. The easiest way to achieve this is using the Configuration Editor in the IIS Mangeger console. Just select each of your sites, and then double click the ‘Configuration Editor’ icon in the middle pane:

 

image.png

 

Once inside this feature, you will be able to access any configuration relating to the website, that is coming either from ApplicationHost.config file or the website’s web.config file. Using the dropdown control from the top of the Configuration Editor window, select the System.WebServer/AspNetCore tag from the list of available tags as shown:

 

image.png

 

Within this section, you will find all of the possible configuration for the ASP.net Core Platforms Handler. Among the settings is one called forwardWindowsAuthToken whish should be set to ‘true’, in order for the module to forward the authentication token to the dotnet.exe process.

 

Next, we need to understand the request flow, which may be the most important part of this tutorial. Nothing gets the point across better than a visual diagram:

image.png

 

In the above diagram, the client browser will:

 

  1. Make a request to http://coreRazor/asyncPoster. The request will be of type GET and will result in the Razor Page executing on the server and sending back some HTML markup to the browser to render.

 

Once the page is displayed, the user will proceed and enter some text in the text area control and press the ‘Echo Text’ button. At this point the interesting stuff starts to happen.

 

  1. The JavaScript engine will send a CORS preflight request to the http://coreWebAPI/api/EchoCors url. The purpose of this request is to make sure that the website allows requests coming from another site (notably http://coreRazor) to be sent and processed. The request materializes as an OPTIONS request on the wire, and the following HTTP headers are sent out:
OPTIONS /api/EchoCors HTTP/1.1
Host: corewebapi
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://corerazor
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
Access-Control-Request-Headers: content-type Accept: */*
Accept-Encoding: gzip, deflate 

 

I have highlighted the important information in gray – the Origin header indicates where the Javascript that is performing the CORS request originated from: http://corerazor. The Access-Control-Request-Method indicates what kind of request the script would like to make (a POST request in this case). According to: https://fetch.spec.whatwg.org/#cors-preflight-fetch, the CORS preflight request has to go out without any authentication. And this is the main problem in our case.

 

If you have followed along with the tutorial up to this point, you will know that Windows Integrated Authentication will reject any requests that are sent without credentials, and the response from the IIS server will be a 401 status code, requesting authentication. This will not allow the JavaScript engine to know if requests from the origin site are allowed to the target backend site and will result in failure.

In order to remedy this, we need to modify the IIS settings, for the backend http://coreWebAPI site to allow anonymous requests in certain cases. This is where a good understanding of the HTTP pipeline in IIS comes in handy, and I would suggest a detour to video number 2 in the IIS Architecture and Components videos I have posted online a while ago: http://linqto.me/n/IISArchitecture . The HTTP processing pipeline is based on events being raised and pieces of code called ‘http modules’ subscribing to the notifications sent out on these events to perform changes to the request or the response.

 

Two pipeline stages are important here: 1) Authentication and 2) Authorization. In the authentication stage, we are trying to understand who the user behind the request is. So, we may choose to say the user is the ‘anonymous user’, and that anonymous authentication should also be turned on. Below are the authentication settings on IIS for the http://coreWebAPI site – note that both Windows Integrated Authentication and Anonymous Authentication are enabled:

 

image.png

 

In the next stage of request processing, the Authorization stage (or Authorization event), we need to make sure we only allow the anonymous requests from CORS preflight and require all other incoming requests have authentication credentials supplied. We can achieve this through Authorization Rules. A default authorization rule granting all users access to the site is already in place and supplied by default by IIS. We will start by modifying this rule to only allow anonymous users, if they send requests that are using the OPTIONS http verb. Below is the target configuration in IIS for this authorization rule:

 

image.png

 

However, this authorization rule is not enough as it will block all other requests. We need a second ‘Allow’ Authorization rule that needs to be introduced. This rule will permit access to the application to a desired group of users – for the demonstration, I have chosen the ‘Users’ group, which should include most authenticated user accounts from the server where the backend application is hosted. In a real-world scenario, we would configure an Active Directory group, such as ‘Domain Users’ if a really large scope was targeted, or some more restrained, like a specific user group.

 

image.png

 

Here is what the web.config file for the backend website will contain following the modifications to the authorization rules:

 

<system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule"
                  resourceType="Unspecified" />
    </handlers>

    <aspNetCore processPath="dotnet" arguments=".\WebApiEcho.dll"
                  stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
        <security>
            <authentication>
                <anonymousAuthentication enabled="true" />
                <windowsAuthentication enabled="true" />
            </authentication>

            <authorization>
                <remove users="*" roles="" verbs="" />
                <add accessType="Allow" users="?" verbs="OPTIONS" />
                <add accessType="Allow" users="" roles="USERS" />
            </authorization>
        </security>
  </system.webServer>

 

These two rules will ensure that: CORS preflight OPTION request rules will be honored without requiring authentication, while all other incoming requests will need to present valid credentials to be allowed through to our ASP.net Core WebAPI application.

If we review the response headers for the CORS preflight request shown earlier, we can see that the server does authorize the receipt of CORS request from the http://coreRazor website:

 

HTTP/1.1 200 OK
Vary: Origin
Server: Kestrel
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Origin: http://corerazor
X-Powered-By: ASP.NET
Content-Length: 0

 

In the highlighted headers, we can see that the server is indicating that CORS requests to this server from the website are allowed if they supply credentials, and if the requests originate from the http://coreRazor site. With this information, the JavaScript engine can now proceed to perform the POST request with JSON content to send to the WebAPI controller for processing.

 

Post Scriptum:

Following the writing of this article, a new module called the IIS CORS module has become available. The module can be downloaded from the following link: https://www.iis.net/downloads/microsoft/iis-cors-module . Documentation on the module is available below, as well as configuration settings: https://docs.microsoft.com/en-us/iis/extensions/cors-module/cors-module-configuration-reference .

 

The way in which the module works is by hooking into the IIS pipeline very early on and intercepting and inspecting OPTIONS requests. If the request is determined to be a CORS preflight request, the request is handled even before the authentication stage of the IIS pipeline can be reached. This allows the module to handle requests even on websites that do not allow Anonymous authentication and then setup the authorization rules as described above. All you have to do is to install and configure the module, bypassing the configuration of the authorization rules.

 

Written by: Paul Cociuba
Reviewed by: Muna AlHasan