Home
%3CLINGO-SUB%20id%3D%22lingo-sub-337482%22%20slang%3D%22en-US%22%3EImplementing%20Multiple%20Identities%20in%20your%20.NET%20Core%20Web%20App%20-%20Part%201%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-337482%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Sep%2003%2C%202018%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3E%3CI%3E%20Authored%20by%20Andreas%20Helland%20%3C%2FI%3E%3C%2FP%3E%3CBR%20%2F%3E%20Software%20developers%20have%20been%20dealing%20with%20the%20concept%20of%20users%20in%20their%20apps%20for%20many%20years%2C%20and%20many%20of%20us%20have%20implemented%20simple%20schemes%20for%20computing%20a%20hash%20of%20the%20password%20and%20rolling%20our%20own%20mechanisms%20and%20identity%20stores%20to%20support%20this.%20I%20don't%20think%20I%20have%20seen%20anyone%20recommend%20as%20a%20best%20practice%20that%20you%20should%20build%20your%20own%20identity%20system%2C%20and%20yet%20this%20is%20what%20people%20have%20been%20doing.%20Not%20saying%20it%20is%20impossible%20to%20do%20so%2C%20but%20even%20though%20you%20know%20what%20SQL%20injection%20is%20and%20have%20guarded%20your%20id%20database%20against%20those%20attacks%20there%20are%20probably%20a%20range%20of%20other%20vulnerabilities%20you%20aren't%20able%20to%20keep%20on%20top%20of%20unless%20you%20have%20more%20resources%20available%20to%20you%20than%20the%20average%20developer.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20On%20the%20bright%20side%2C%20I%20have%20observed%20a%20push%20towards%20using%20Azure%20AD%20as%20the%20identity%20system%20many%20places%20in%20the%20past%20couple%20of%20years.%20Of%20course%20-%20this%20could%20be%20just%20me%20and%20the%20people%2Fcompanies%20I%20run%20into.%20But%20in%20general%20people%20want%20SSO%20which%20requires%20some%20centralized%20component%20to%20be%20involved%2C%20and%20for%20corporate%20use%20Active%20Directory%20and%2For%20Azure%20Active%20Directory%20frequently%20comes%20into%20play.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20basic%20single%20Azure%20AD%20tenant%20authentication%20scenario%20is%20something%20most%20developers%20should%20be%20able%20to%20implement%20fairly%20easily%20based%20on%20the%20samples%20provided%20by%20Microsoft.%20Call%20it%20the%20happy%20path%20scenario%20if%20you%20will.%20Unfortunately%20a%20lot%20of%20the%20use%20cases%20out%20there%20are%20not%20the%20basic%20single%20tenant%20setup.%20Maybe%20you%20want%20multi-tenant%20support.%20Maybe%20you%20need%20support%20for%20social%20identities%20like%20Facebook%20and%20Google.%20And%20maybe%20you%20want%20to%20provide%20different%20frontends%20for%20consumers%2C%20partners%2C%20etc.%20That's%20when%20things%20start%20to%20get%20more%20complicated%20%3A)%3C%2Fimg%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Even%20though%20I%20have%20my%20Microsoft%20goggles%20on%20I%20have%20no%20problem%20admitting%20there%20are%20great%20tools%20out%20there%2C%20made%20by%20other%20companies%2C%20for%20managing%20identities%20and%20securing%20resources.%20However%20since%20I%20have%20invested%20time%20and%20effort%20into%20the%20Azure%20technology%20stack%20it%20makes%20sense%20for%20me%20to%20try%20to%20implement%20this%20with%20MS%20tech%2C%20so%20I%20thought%20I%20would%20take%20a%20stab%20at%20building%20out%20my%20own%20sample%20using%20Azure%20AD%20B2C%20for%20Identity%20as%20a%20Service%20(IdaaS).%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20I%20have%20done%20a%20couple%20of%20posts%20on%20AAD%20B2C%20before%2C%20and%20of%20course%20you%20will%20also%20find%20info%20from%20Microsoft%20themselves%20on%20what%20Azure%20AD%20B2C%20is%20so%20I'm%20not%20going%20to%20do%20an%20introductory%20part%20on%20what%20the%20service%20can%20do.%20The%20name%20of%20the%20service%20does%20it%20no%20favors%20though%20since%20it%20seems%20to%20imply%20it's%20all%20about%20consumers.%20The%20identity%20teams%20in%20Redmond%20are%20now%20more%20often%20referring%20to%20%22external%20identities%22%20which%20is%20a%20better%20term%20in%20my%20opinion.%20(I%20would%20love%20to%20see%20it%20being%20a%20more%20generic%20%22Azure%20AD%20Dev%20Edition%22%20without%20a%20distinction%20between%20B2C%20and%20non-B2C%2C%20but%20it's%20not%20quite%20there%20yet.%20And%20maybe%20it%20needn't%20be%20either%3B%20that%20is%20however%20an%20entirely%20different%20discussion%20we%20will%20not%20go%20into%20now.)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20If%20you%20want%20to%20learn%20the%20more%20basic%20parts%20of%20AAD%20B2C%2C%20and%20for%20that%20sake%20some%20more%20advanced%20things%20too%2C%20Microsoft%20has%20a%20thorough%20ebook%20you%20can%20go%20through%3A%20%3CA%20href%3D%22https%3A%2F%2Faka.ms%2FlearnAADB2C%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Faka.ms%2FlearnAADB2C%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20And%20thus%20there%20is%20no%20need%20for%20further%20explanations%20right%3F%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74272i7C940ACF53F7E3CF%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Thing%20is%20-%20before%20you%20start%20hacking%20the%20necessary%20pieces%20of%20code%20into%20your%20solution%20you%20need%20a%20strategy%20of%20sorts%20so%20that%20is%20where%20we%20will%20start.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3EWhich%20identity%20providers%20(IdPs)%20will%20you%20support%3F%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20Do%20you%20want%20to%20support%20primarily%20social%20identities%20like%20Facebook%2C%20Google%2C%20etc.%20or%20do%20you%20want%20to%20primarly%20support%20corporate%20identities%3F%20Or%20a%20mix%3F%20Only%20Azure%20AD%20tenants%20or%20other%20corporate-based%20ones%20as%20well%3F%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20adding%20of%20IdPs%20is%20entirely%20independent%20of%20your%20code%2C%20so%20that%20should%20be%20done%20in%20the%20AAD%20B2C%20portal%20before%20you%20start.%20You%20don't%20need%20to%20add%20all%20at%20once%2C%20but%20with%20none%20defined%20things%20are%20not%20going%20to%20work%20so%20add%20at%20least%20one.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Google%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Factive-directory-b2c-setup-goog-app%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Factive-directory-b2c-setup-goog-app%20%3C%2FA%3E%20%3CBR%20%2F%3E%20Facebook%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Factive-directory-b2c-setup-fb-app%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Factive-directory-b2c-setup-fb-app%20%3C%2FA%3E%20%3CBR%20%2F%3E%20Microsoft%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Factive-directory-b2c-setup-msa-app%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Factive-directory-b2c-setup-msa-app%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20For%20adding%20corporate%20identities%20I%20would%20recommend%20using%20the%20OpenID%20Connect%20option%20if%20you%20can.%20Both%20Azure%20AD%20and%20ADFS%20can%20be%20added%20this%20way%2C%20and%20it%20is%20easier%20than%20using%20custom%20policies.%20(More%20on%20that%20topic%20later%20on.)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Custom%20OIDC%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Factive-directory-b2c-setup-oidc-idp%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Factive-directory-b2c-setup-oidc-idp%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20third%20of%20the%20major%20options%20is%20local%20accounts%20-%20these%20are%20identities%20defined%20locally%20in%20Azure%20AD%20B2C%20and%20provisioned%20by%20the%20AAD%20B2C%20service%20natively.%20Some%20users%20will%20want%20to%20silo%20their%20different%20logins%20for%20various%20reasons.%20Should%20you%20cater%20for%20these%20users%20as%20well%3F%20Personally%20I%20like%20having%20this%20option%2C%20but%20this%20is%20a%20design%20point%20you%20need%20to%20consider%20specifically%20for%20your%20use%20case.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Technically%20speaking%20the%20local%20accounts%20are%20also%20AAD%20accounts%2C%20but%20in%20a%20B2C%20version.%20In%20a%20regular%20AAD%20tenant%20users%20are%20assumed%20to%20be%20working%20for%20the%20same%20company%20-%20this%20could%20of%20course%20include%20external%20consultants%20as%20well%2C%20but%20users%20are%20aware%20of%20each%20other%20and%20who%20the%20company%20entity%20owning%20the%20identities%20is.%20This%20means%20that%20you%20have%20things%20like%20a%20Global%20Address%20List%20(GAL)%20where%20you%20can%20look%20up%20the%20phone%20number%20of%20other%20users%20and%20similar%20things.%20In%20an%20system%20where%20users%20are%20all%20external%20and%20independent%20of%20each%20other%20this%20would%20not%20be%20ok%2C%20and%20would%20probably%20violate%20several%20privacy%20laws%2C%20so%20there%20are%20restrictions%20in%20place%20backend%20that%20prevents%20one%20user%20from%20looking%20up%20the%20details%20of%20another%20user.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Being%20AAD%20accounts%20means%20you%20as%20the%20tenant%20owner%20and%20app%20developer%20can%20still%20use%20things%20like%20the%20Graph%20API%20to%20query%20the%20records%20and%20other%20actions%2C%20so%20it's%20still%20better%20than%20rolling%20your%20own%20database%20for%20local%20accounts.%20(As%20a%20side%20note%20-%20the%20MS%20Graph%20is%20not%20supported%20yet%2C%20and%20you%20will%20need%20to%20use%20the%20Azure%20AD%20Graph.)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20How%20will%20the%20landing%20page%20of%20the%20SignUp%20and%2For%20SignIn%20experience%20be%3F%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20Will%20you%20leave%20the%20choice%20of%20identity%20entirely%20up%20to%20the%20user%2C%20or%20will%20you%20guide%20(with%20more%20or%20less%20force)%2C%20towards%20specific%20options%3F%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20For%20instance%20you%20could%20have%20a%20page%20asking%20%22Are%20you%20a%20partner%20or%20consumer%22%20and%20direct%20partners%20towards%20corporate%20identities%20whereas%20consumers%20will%20only%20get%20to%20choose%20between%20different%20social%20identities.%20At%20the%20same%20time%20it%20could%20be%20you%20have%20one-man%20shops%20as%20partners%20that%20aren't%20using%20Office%20365%20and%20Azure%20AD%2C%20and%20prefer%20a%20Google%20account%20instead.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20You%20could%20also%20have%20an%20automated%20flow%20once%20you%20know%20the%20email%20address%20of%20the%20user%20through%20a%20process%20called%20home%20realm%20discovery.%20In%20this%20case%20you%20could%20have%20users%20type%20in%20their%20email%20address%20first%2C%20and%20if%20it's%20%3CEM%3E%20joe%40contoso.com%20%3C%2FEM%3E%20send%20them%20directly%20to%20the%20Contoso%20login%20page.%20Do%20keep%20in%20mind%20to%20that%20there%20are%20some%20open-ended%20scenarios%20-%20just%20because%20an%20email%20address%20ends%20in%20gmail.com%20doesn't%20mean%20the%20identity%20has%20to%20be%20a%20Google%20account.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3EWhat%20needs%20to%20be%20customized%2C%20and%20what%20will%20work%20out%20of%20the%20box%3F%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20This%20is%20somewhat%20AAD%20B2C-specific%20since%20custom%20policies%20are%20an%20important%20part%20of%20achieving%20more%20advanced%20scenarios.%20You%20could%20however%20argue%20that%20some%20of%20the%20considerations%20would%20be%20valid%20even%20without%20AAD%20B2C.%20More%20specialized%20use%20cases%20will%20require%20more%20custom%20work%20with%20or%20without%20AAD%20B2C.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Take%20something%20like%20resetting%20passwords.%20This%20is%20a%20fairly%20common%20use%20case%20on%20web%20pages%20that%20rely%20on%20logging%20in.%20But%20if%20the%20user%20logs%20in%20with%20Facebook%20it's%20not%20like%20you%20can%20perform%20a%20reset%20-%20that%20would%20be%20something%20Facebook%20would%20need%20to%20provide.%20If%20you%20enable%20local%20accounts%20in%20AAD%20B2C%20you%20are%20responsible%20for%20that%20experience.%20Sure%2C%20you%20would%20%22outsource%22%20the%20task%20to%20Azure%20AD%2C%20but%20you%20would%20need%20to%20account%20for%20it%20in%20your%20UI.%20Luckily%20this%20is%20a%20simple%20feature%20that%20can%20be%20implemented%20in%20a%20standard%20policy%20in%20B2C%2C%20and%20as%20long%20as%20you%20send%20the%20user%20to%20the%20right%20page%20in%20the%20UI%20you%20might%20not%20need%20all%20the%20extras%20custom%20policies%20bring%20to%20the%20table.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20This%20brings%20out%20a%20key%20point%20with%20Azure%20AD%20B2C%20-%20if%20the%20standard%20template%20provides%20what%20you%20need%20you%20should%20not%20build%20a%20custom%20policy%20%22just%20because%20you%20can%22.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20What%20would%20be%20a%20valid%20use%20case%20for%20a%20custom%20policy%20then%3F%20Maybe%20you%20need%20to%20validate%20some%20of%20the%20inputs%20the%20user%20types%20in%20when%20signing%20up%20and%20that%20validation%20needs%20to%20be%20done%20through%20an%20API%20call.%20That%20would%20require%20a%20custom%20policy.%20Maybe%20the%20user%20needs%20a%20code%20you%20have%20provided%20in%20advance%20to%20be%20able%20to%20sign%20up.%20That%20would%20require%20a%20custom%20policy.%20Basically%2C%20if%20you%20don't%20find%20it%20in%20the%20UI%20in%20the%20Azure%20Portal%20you%20need%20to%20think%20custom.%20Let's%20be%20realistic%20though%20-%20there%20are%20of%20course%20limits%20to%20what%20you%20can%20do%20in%20the%20custom%20space%20as%20well.%20You%20can%20do%20more%2C%20but%20you%20can't%20do%20everything%20%3A)%3C%2Fimg%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Authorization%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20Authentication%20is%20the%20easy%20part.%20Well%2C%20easy%20might%20be%20stretching%20it%3B%20there%20are%20as%20you%20can%20see%20plenty%20of%20considerations%20on%20how%20to%20acquire%20your%20golden%20ticket%20(aka%20token).%20But%20once%20the%20user%20has%20a%20token%20then%20what%3F%20If%20you%20have%20a%20setup%20with%20partners%20and%20end-users%20it's%20probably%20fair%20to%20assume%20that%20they%20should%20not%20be%20able%20to%20access%20the%20same%20content.%20You%20will%20probably%20need%20to%20have%20different%20roles%2C%20and%20use%20that%20as%20a%20filtering%20mechanism.%20Plan%20this%20out%20before%20you%20hack%20along.%20(Figuring%20out%20details%20like%20whether%20you%20need%20two%20roles%20on%20the%20partner%20level%2C%20say%20%22Partner%20Admin%22%20and%20%22Partner%20User%22%2C%20isn't%20needed.%20You%20can%20do%20that%20later%20on.)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20Our%20plan%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20With%20those%20considerations%20out%20of%20the%20way%20let's%20spec%20out%20what%20we%20want%20to%20implement.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20We%20will%20build%20a%20web%20app%20that%20will%20provide%20services%20to%20employees%2C%20partners%2C%20and%20end-users%2Fcustomers.%20For%20this%20we%20will%20support%20both%20Azure%20AD%2C%20social%20accounts%2C%20and%20local%20accounts.%20The%20web%20app%20will%20be%20implemented%20in%20.NET%20Core%202.1.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20I%20will%20admit%20that%20I%20was%20inspired%20by%20a%20Microsoft%20demo%3A%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure-Samples%2Factive-directory-external-identities-woodgrove-demo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fgithub.com%2FAzure-Samples%2Factive-directory-external-identities-woodgrove-demo%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20I%20don't%20intend%20to%20copy%20it%20outright%20though%2C%20fact%20is%20I%20didn't%20copy%20any%20of%20the%20source%2C%20and%20I%20will%20deliberately%20create%20a%20less%20polished%20version%20with%20regards%20to%20UI%20to%20focus%20on%20specific%20parts.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20We%20will%20place%20as%20much%20of%20the%20authentication%20and%20authorization%20logic%20in%20AAD%20B2C%20as%20we%20can%20keeping%20the%20middleware%20configuration%20clean.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20first%20step%20is%20to%20create%20a%20new%20project%20in%20Visual%20Studio%202017%20using%20the%20%3CEM%3E%20ASP.NET%20Core%20Web%20Application%20%3C%2FEM%3E%20template.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74273i925E0DBE7E552B5A%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Notice%20that%20authentication%20is%20set%20to%20%3CEM%3E%20No%20Authentication%20%3C%2FEM%3E%20as%20we%20will%20build%20in%20that%20manually.%20Also%20make%20sure%20that%20%3CEM%3E%20Configure%20for%20HTTPS%20%3C%2FEM%3E%20is%20checked%20-%20this%20is%20needed%20for%20authentication%20services.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20As%20always%2C%20make%20sure%20the%20basics%20are%20working%20by%20hitting%20F5%20and%20verify%20that%20the%20app%20builds%20and%20deploys%20without%20issues.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Before%20producing%20more%20code%20we%20will%20head%20over%20the%20Azure%20AD%20B2C%20Portal%20to%20create%20a%20few%20pieces%20and%20make%20sure%20things%20are%20working%20there.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3EAzure%20AD%20B2C%20Basics%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20If%20you%20don't%20have%20an%20AAD%20B2C%20tenant%20already%20that%20would%20be%20the%20first%20step.%20Microsoft%20details%20this%20here%3A%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Ftutorial-create-tenant%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Ftutorial-create-tenant%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Make%20sure%20that%20you%20have%20%3CEM%3E%20Local%20accounts%20%3C%2FEM%3E%20listed%20in%20the%20%3CEM%3E%20Identity%20providers%20%3C%2FEM%3E%20section%3A%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74275i4F7CEE0CB76B98E1%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20tooltip%20explains%20that%20you%20can%20choose%20between%20%3CEM%3E%20Email%20%3C%2FEM%3E%20and%20%3CEM%3E%20Username%20%3C%2FEM%3E%20for%20these%20accounts%20-%20I%20will%20go%20with%20email.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Let's%20create%20a%20very%20basic%20set%20of%20policies.%20We%20just%20want%20to%20make%20sure%20that%20we%20can%20sign%20up%20and%20sign%20in%20first.%20Move%20down%20to%20%3CEM%3E%20Sign-up%20policies%20%3C%2FEM%3E%20and%20click%20%3CEM%3E%20Add%20%3C%2FEM%3E%20%3A%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74277i57E82F0F64A432F1%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Check%20%3CEM%3E%20Email%20signup%20%3C%2FEM%3E%20in%20the%20%3CEM%3E%20Identity%20providers%20%3C%2FEM%3E%20section%3A%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74278iB673FCFA7013E194%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CEM%3E%20Sign-up%20attributes%20%3C%2FEM%3E%20are%20the%20parameters%20we%20want%20to%20collect%20from%20the%20user%20when%20they%20create%20their%20account.%20You%20can%20check%20every%20one%20of%20them%20if%20you%20like%2C%20but%20that%20means%20more%20typing%20when%20testing%20%3A)%3C%2Fimg%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74280i5566E944C5926F73%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Application%20claims%20are%20the%20parameters%20you%20want%20to%20be%20part%20of%20the%20token%20you%20issue%20when%20someone%20signs%20in%3A%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74282i3433BA1A66357080%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20What%20is%20the%20difference%3F%20Well%2C%20some%20attributes%20does%20not%20make%20sense%20to%20collect%20from%20the%20user%2C%20but%20makes%20sense%20for%20the%20app.%20Other%20attributes%20are%20only%20relevant%20to%20have%20on%20file%20backend.%20The%20sign%20up%20attributes%20get%20persisted%20to%20the%20user%20object%2C%20but%20you%20can%20also%20have%20ephemeral%20attributes%20that%20only%20live%20in%20the%20context%20of%20a%20session.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20You%20can%20add%20additional%20custom%20attributes%20if%20you%20like%2C%20but%20you're%20still%20somewhat%20limited%20until%20you%20go%20for%20custom%20polices.%20For%20now%20we%20don't%20need%20any%20extras.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20We%20don't%20need%20%3CEM%3E%20Multifactor%20authentication%20%3C%2FEM%3E%20or%20%3CEM%3E%20Page%20UI%20customization%20%3C%2FEM%3E%20for%20now%20either%20so%20hit%20%3CEM%3E%20Create%20%3C%2FEM%3E%20on%20the%20bottom%20of%20the%20page.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20To%20use%20policies%2C%20or%20B2C%20in%20any%20way%2C%20you%20need%20to%20create%20an%20application.%20Move%20to%20%3CEM%3E%20Applications%20%3C%2FEM%3E%20and%20click%20%3CEM%3E%20Add%20%3C%2FEM%3E%20.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Check%20%3CEM%3E%20Yes%20%3C%2FEM%3E%20to%20it%20being%20a%20Web%20App%20%2F%20Web%20API%2C%20and%20use%20%3CA%20href%3D%22https%3A%2F%2Fjwt.ms%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fjwt.ms%20%3C%2FA%3E%20as%20the%20reply%20url.%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74283iD5E0457B2A87FB79%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Go%20back%20to%20the%20%3CEM%3E%20Sign-up%20policies%20%3C%2FEM%3E%20section%20and%20click%20the%20policy%20you%20created%20a%20couple%20of%20moments%20ago.%20Select%20the%20application%20you%20just%20created%2C%20and%20make%20sure%20%3CEM%3E%20jwt.ms%20%3C%2FEM%3E%20is%20the%20reply%20url%20before%20clicking%20the%20%3CEM%3E%20Run%20now%20%3C%2FEM%3E%20button.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Now%20you%20get%20to%20sign%20up%20for%20an%20account%20in%20your%20tenant%3A%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74285iA060536EC3EB5135%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20After%20creating%20the%20account%20you're%20automatically%20redirected%20to%20a%20page%20that%20shows%20you%20your%20token%20and%20the%20contents%20of%20it.%20(When%20you%20troubleshoot%20policies%20you%20will%20appreciate%20this%20feature.)%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74286i3CA42950BD007239%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Wasn't%20that%20easy%3F%20You're%20creating%20identities%20and%20testing%20them%20without%20a%20single%20line%20of%20code%20written!%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Next%20you%20should%20create%20a%20%3CEM%3E%20Sign-in%20policy%20%3C%2FEM%3E%20%2C%20and%20a%20%3CEM%3E%20Password%20reset%20policy%20%3C%2FEM%3E%20.%20I%20made%20the%20Sign-in%20more%20or%20less%20similar%20to%20Sign-up.%20With%20the%20password%20reset%20you%20don't%20really%20have%20that%20many%20options.%20Note%20that%20password%20reset%20only%20works%20with%20local%20accounts.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Actually%2C%20let's%20pull%20one%20more%20trick%20with%20the%20AAD%20B2C%20built-in%20policies.%20Head%20to%20%3CEM%3E%20Sign-up%20or%20sign-in%20policies%20%3C%2FEM%3E%20and%20fill%20the%20forms%20out%20the%20same%20way.%20A%20%22SuSi%22%20policy%20produces%20a%20screen%20where%20the%20user%20chooses%20if%20they%20want%20to%20sign%20up%20or%20sign%20in%20thus%20sparing%20you%20a%20couple%20of%20calories%20as%20a%20coder%20when%20you're%20implementing%20your%20login%20links.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3EConfiguring%20authentication%20in%20your%20web%20app%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20Now%20we%20can%20try%20to%20implement%20this%20in%20our%20own%20app.%20Go%20back%20to%20the%20app%20you%20registered%20in%20the%20B2C%20Portal.%20Copy%20the%20application%20id%20to%20Notepad%20or%20something.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20We%20also%20need%20to%20register%20our%20reply%20url%20for%20our%20app%20-%20so%20check%20with%20port%20your%20app%20runs%20on%3A%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74288iCD4797AAF67E55A3%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Mine%20is%20running%20on%20port%2044315%20as%20you%20can%20see.%20So%20register%20%3CEM%3E%20https%3A%2F%2Flocalhost%3A44315%2Fsignin-oidc%20%3C%2FEM%3E%20as%20a%20reply%20url%3A%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74290i463C25EF310E8115%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20What%20I%20didn't%20show%20you%20when%20we%20created%20the%20web%20app%20was%20that%20there%20is%20actually%20a%20wizard%20very%20suited%20for%20Azure%20AD%20B2C%20in%20Visual%20Studio%202017%3A%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74292i7664B8EE83FDFA1D%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20However%20you%20wouldn't%20be%20able%20to%20actually%20fill%20it%20out%20had%20you%20not%20gone%20through%20the%20AAD%20B2C%20portal%20first.%20Let's%20add%20the%20code%20that%20would%20have%20been%20generated%20the%20manual%20way.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20First%20you%20need%20to%20add%20the%20following%20NuGet%20package%3A%20%3CBR%20%2F%3E%20%3CEM%3E%20Microsoft.AspNetCore.Authentication.AzureADB2C.UI%20%3C%2FEM%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20authentication%20is%20configured%20in%20%3CEM%3E%20Startup.cs%20%3C%2FEM%3E%20.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Add%20two%20usings%3A%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20using%20Microsoft.AspNetCore.Authentication%3B%20%3CBR%20%2F%3E%20using%20Microsoft.AspNetCore.Authentication.AzureADB2C.UI%3B%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Add%20the%20following%20to%20the%20ConfigureServices%20method%3A%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)%20%3CBR%20%2F%3E%20.AddAzureADB2C(options%20%3D%26gt%3B%20Configuration.Bind(%22AzureADB2C%22%2C%20options))%3B%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Add%20one%20line%20to%20the%20%3CEM%3E%20Configure%20%3C%2FEM%3E%20method%3A%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20app.UseAuthentication()%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Time%20to%20add%20the%20settings%20for%20our%20AAD%20B2C%20tenant%20-%20these%20can%20be%20found%20in%20%3CEM%3E%20appsettings.json%20%3C%2FEM%3E%20.%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20%22AzureAdB2C%22%3A%20%7B%20%3CBR%20%2F%3E%20%22Instance%22%3A%20%22%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2Ftfp%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2Ftfp%2F%3C%2FA%3E%22%2C%20%3CBR%20%2F%3E%20%22ClientId%22%3A%20%22applicationId-from-portal%22%2C%20%3CBR%20%2F%3E%20%22CallbackPath%22%3A%20%22%2Fsignin-oidc%22%2C%20%3CBR%20%2F%3E%20%22Domain%22%3A%20%22yourtenant.onmicrosoft.com%22%2C%20%3CBR%20%2F%3E%20%22SignUpSignInPolicyId%22%3A%20%22B2C_1_ContosoLocalSuSi%22%2C%20%3CBR%20%2F%3E%20%22ResetPasswordPolicyId%22%3A%20%22B2C_1_ContosoLocalPasswordReset%22%2C%20%3CBR%20%2F%3E%20%22EditProfilePolicyId%22%3A%20%22%22%20%3CBR%20%2F%3E%20%7D%2C%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20We%20also%20need%20a%20UI%20with%20a%20button%20for%20signing%20in.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Add%20a%20file%20called%20%3CEM%3E%20_LoginPartial.cshtml%20%3C%2FEM%3E%20with%20the%20following%20contents%3A%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20%40using%20System.Security.Principal%20%3CBR%20%2F%3E%20%40using%20Microsoft.AspNetCore.Authentication.AzureADB2C.UI%20%3CBR%20%2F%3E%20%40using%20Microsoft.Extensions.Options%20%3CBR%20%2F%3E%20%40inject%20IOptionsMonitor%3CAZUREADB2COPTIONS%3E%20AzureADB2COptions%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%40%7B%20%3CBR%20%2F%3E%20var%20options%20%3D%20AzureADB2COptions.Get(AzureADB2CDefaults.AuthenticationScheme)%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%40if%20(User.Identity.IsAuthenticated)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CUL%20class%3D%22%26quot%3Bnav%22%20navbar-nav%3D%22%22%20navbar-right%3D%22%22%3E%20%3CBR%20%2F%3E%20%40if%20(!string.IsNullOrEmpty(options.EditProfilePolicyId))%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CLI%3E%3CA%20asp-area%3D%22%26quot%3BAzureADB2C%26quot%3B%22%20asp-controller%3D%22%26quot%3BAccount%26quot%3B%22%20asp-action%3D%22%26quot%3BEditProfile%26quot%3B%22%20target%3D%22_blank%22%3EHello%20%40User.Identity.Name!%3C%2FA%3E%3C%2FLI%3E%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20else%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CLI%20class%3D%22%26quot%3Bnavbar-text%26quot%3B%22%3EHello%20%40User.Identity.Name!%3C%2FLI%3E%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CLI%3E%3CA%20asp-area%3D%22%26quot%3BAzureADB2C%26quot%3B%22%20asp-controller%3D%22%26quot%3BAccount%26quot%3B%22%20asp-action%3D%22%26quot%3BSignOut%26quot%3B%22%20target%3D%22_blank%22%3ESign%20out%3C%2FA%3E%3C%2FLI%3E%20%3CBR%20%2F%3E%20%3C%2FUL%3E%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20else%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%3CUL%20class%3D%22%26quot%3Bnav%22%20navbar-nav%3D%22%22%20navbar-right%3D%22%22%3E%20%3CBR%20%2F%3E%20%3CLI%3E%3CA%20asp-area%3D%22%26quot%3BAzureADB2C%26quot%3B%22%20asp-controller%3D%22%26quot%3BAccount%26quot%3B%22%20asp-action%3D%22%26quot%3BSignIn%26quot%3B%22%20target%3D%22_blank%22%3ESign%20in%3C%2FA%3E%3C%2FLI%3E%20%3CBR%20%2F%3E%20%3C%2FUL%3E%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Add%20a%20partial%20to%20%3CEM%3E%20_Layout.cshtml%20%3C%2FEM%3E%20like%20this%3A%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20%3CDIV%20class%3D%22%26quot%3Bnavbar-collapse%22%20collapse%3D%22%22%3E%20%3CBR%20%2F%3E%20%3CUL%20class%3D%22%26quot%3Bnav%22%20navbar-nav%3D%22%22%3E%20%3CBR%20%2F%3E%20%3CLI%3E%3CA%20asp-area%3D%22%26quot%3B%26quot%3B%22%20asp-controller%3D%22%26quot%3BHome%26quot%3B%22%20asp-action%3D%22%26quot%3BIndex%26quot%3B%22%20target%3D%22_blank%22%3EHome%3C%2FA%3E%3C%2FLI%3E%20%3CBR%20%2F%3E%20%3CLI%3E%3CA%20asp-area%3D%22%26quot%3B%26quot%3B%22%20asp-controller%3D%22%26quot%3BHome%26quot%3B%22%20asp-action%3D%22%26quot%3BAbout%26quot%3B%22%20target%3D%22_blank%22%3EAbout%3C%2FA%3E%3C%2FLI%3E%20%3CBR%20%2F%3E%20%3CLI%3E%3CA%20asp-area%3D%22%26quot%3B%26quot%3B%22%20asp-controller%3D%22%26quot%3BHome%26quot%3B%22%20asp-action%3D%22%26quot%3BContact%26quot%3B%22%20target%3D%22_blank%22%3EContact%3C%2FA%3E%3C%2FLI%3E%20%3CBR%20%2F%3E%20%3C%2FUL%3E%20%3CBR%20%2F%3E%20%3CPARTIAL%20name%3D%22%26quot%3B_LoginPartial%26quot%3B%22%3E%3C%2FPARTIAL%3E%20%3CBR%20%2F%3E%20%3C%2FDIV%3E%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20If%20you%20hit%20F5%20now%20you%20will%20see%20a%20%3CEM%3E%20Sign%20In%20%3C%2FEM%3E%20button%2C%20and%20clicking%20it%20will%20take%20you%20to%20AAD%20B2C%20and%20return%20you%20to%20your%20web%20site%20afterwards.%20Nice%20and%20simple.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Hold%20on%2C%20it%20just%20says%20%22Hello%20!%22%20-%20there%20is%20no%20name%20there.%20Yes%2C%20due%20to%20how%20I%20configured%20my%20policies%20it%20doesn't%20line%20up%20with%20the%20claims%20expected%20to%20print%20out%20properly.%20This%20is%20purely%20a%20cosmetic%20issue%20that%20we%20will%20not%20care%20about%20right%20now.%20You%20are%20logged%20in.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20In%20the%20web%20app%20as%20it%20is%20now%20this%20sign%20in%20action%20does%20not%20serve%20any%20purpose.%20You%20can%20browse%20everything%20whether%20you're%20authenticated%20or%20not.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20While%20it%20is%20a%20silly%20example%20let's%20say%20only%20authenticated%20users%20can%20view%20our%20contact%20info.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3EConfiguring%20authorization%20in%20your%20web%20app%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20Open%20up%20%3CEM%3E%20HomeController.cs%20%3C%2FEM%3E%20and%20add%20an%20Authorize%20attribute%20to%20your%20Contact%20action%3A%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20%5BAuthorize%5D%20%3CBR%20%2F%3E%20public%20IActionResult%20Contact()%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20...%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20And%20add%20%3CEM%3E%20using%20Microsoft.AspNetCore.Authorization%3B%20%3C%2FEM%3E%20along%20with%20the%20other%20usings.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20If%20you%20try%20to%20click%20Contact%20without%20being%20logged%20in%20you%20will%20be%20taken%20to%20the%20sign-in%20page%20to%20type%20in%20your%20credentials.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Ok%2C%20so%20now%20we%20have%20a%20very%20basic%20model%20for%20authentication%20(AuthN)%20and%20authorization%20(AuthZ)%20in%20our%20web%20app.%20We%20are%20however%20still%20a%20bit%20off%20with%20regards%20to%20our%20original%20plan%20-%20what%20now%3F%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20I'll%20add%20views%20for%20partners%2C%20customers%20and%20employees%20to%20my%20app.%20%3CBR%20%2F%3E%20Add%20methods%20in%20%3CEM%3E%20HomeController.cs%20%3C%2FEM%3E%20%3A%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20public%20IActionResult%20Partner()%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20ViewData%5B%22Message%22%5D%20%3D%20%22Howdy%20partner!%22%3B%20%3CBR%20%2F%3E%20return%20View()%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20public%20IActionResult%20Customer()%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20ViewData%5B%22Message%22%5D%20%3D%20%22Welcome%20dear%20customer.%20How%20may%20we%20help%20you%20today.%22%3B%20%3CBR%20%2F%3E%20return%20View()%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20public%20IActionResult%20Employee()%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20ViewData%5B%22Message%22%5D%20%3D%20%22Get%20back%20to%20work!%22%3B%20%3CBR%20%2F%3E%20return%20View()%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20I%20use%20scaffolding%20to%20create%20views%20(right-click%20method%2C%20select%20%3CEM%3E%20Add%20View%20%3C%2FEM%3E%20%2C%20and%20use%20the%20%3CEM%3E%20Empty%20%3C%2FEM%3E%20template.)%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Add%20a%20few%20menu%20items%20to%20%3CEM%3E%20_Layout.cshtml%20%3C%2FEM%3E%20%3A%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20%3CUL%20class%3D%22%26quot%3Bnav%22%20navbar-nav%3D%22%22%3E%20%3CBR%20%2F%3E%20%3CLI%3E%3CA%20asp-area%3D%22%26quot%3B%26quot%3B%22%20asp-controller%3D%22%26quot%3BHome%26quot%3B%22%20asp-action%3D%22%26quot%3BIndex%26quot%3B%22%20target%3D%22_blank%22%3EHome%3C%2FA%3E%3C%2FLI%3E%20%3CBR%20%2F%3E%20%3CLI%3E%3CA%20asp-area%3D%22%26quot%3B%26quot%3B%22%20asp-controller%3D%22%26quot%3BHome%26quot%3B%22%20asp-action%3D%22%26quot%3BAbout%26quot%3B%22%20target%3D%22_blank%22%3EAbout%3C%2FA%3E%3C%2FLI%3E%20%3CBR%20%2F%3E%20%3CLI%3E%3CA%20asp-area%3D%22%26quot%3B%26quot%3B%22%20asp-controller%3D%22%26quot%3BHome%26quot%3B%22%20asp-action%3D%22%26quot%3BContact%26quot%3B%22%20target%3D%22_blank%22%3EContact%3C%2FA%3E%3C%2FLI%3E%20%3CBR%20%2F%3E%20%3CLI%3E%3CA%20asp-area%3D%22%26quot%3B%26quot%3B%22%20asp-controller%3D%22%26quot%3BHome%26quot%3B%22%20asp-action%3D%22%26quot%3BPartner%26quot%3B%22%20target%3D%22_blank%22%3EPartner%3C%2FA%3E%3C%2FLI%3E%20%3CBR%20%2F%3E%20%3CLI%3E%3CA%20asp-area%3D%22%26quot%3B%26quot%3B%22%20asp-controller%3D%22%26quot%3BHome%26quot%3B%22%20asp-action%3D%22%26quot%3BCustomer%26quot%3B%22%20target%3D%22_blank%22%3ECustomer%3C%2FA%3E%3C%2FLI%3E%20%3CBR%20%2F%3E%20%3CLI%3E%3CA%20asp-area%3D%22%26quot%3B%26quot%3B%22%20asp-controller%3D%22%26quot%3BHome%26quot%3B%22%20asp-action%3D%22%26quot%3BEmployee%26quot%3B%22%20target%3D%22_blank%22%3EEmployee%3C%2FA%3E%3C%2FLI%3E%20%3CBR%20%2F%3E%20%3C%2FUL%3E%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20How%20to%20make%20sure%20only%20partners%20can%20access%20the%20partner%20page%20and%20so%20on%3F%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20We%20will%20use%20.NET%20Core%20Role-based%20authorization%3A%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Faspnet%2Fcore%2Fsecurity%2Fauthorization%2Froles%3Fview%3Daspnetcore-2.1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Faspnet%2Fcore%2Fsecurity%2Fauthorization%2Froles%3Fview%3Daspnetcore-2.1%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20This%20approach%20will%20work%2C%20in%20the%20sense%20that%20you%20will%20get%20access%20denied%20even%20if%20you%20sign%20in.%20Having%20role%20checks%20doesn't%20work%20if%20you%20do%20not%20have%20a%20way%20to%20get%20users%20into%20those%20roles%20which%20sort%20of%20breaks%20the%20basic%20setup%E2%80%A6%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20We're%20using%20claims-based%20authentication%20though%2C%20and%20have%20claims%20we%20are%20supposed%20to%20be%20able%20to%20check%20-%20so%20maybe%20we%20can%20do%20something%20with%20those%3F%20%3CBR%20%2F%3E%20Probably%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Faspnet%2Fcore%2Fsecurity%2Fauthorization%2Fclaims%3Fview%3Daspnetcore-2.1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Faspnet%2Fcore%2Fsecurity%2Fauthorization%2Fclaims%3Fview%3Daspnetcore-2.1%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20If%20we%20can%20add%20the%20right%20claim%20we%20can%20make%20a%20policy%20for%20it.%20There%20doesn't%20seem%20to%20be%20a%20ready-made%20attribute%20for%20this%2C%20but%20luckily%20AAD%20B2C%20lets%20us%20customize%20this%20with%20little%20effort%20by%20adding%20custom%20attributes%20so%20go%20back%20to%20the%20AAD%20B2C%20Portal.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Navigate%20to%20the%20%3CEM%3E%20User%20attributes%20%3C%2FEM%3E%20section%20and%20click%20%3CEM%3E%20Add%20%3C%2FEM%3E%20.%20Add%20an%20attribute%20called%20%3CEM%3E%20Role%20%3C%2FEM%3E%20%3A%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74293iB81AB9F5E681C8FF%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Edit%20your%20policy%20to%20include%20this%20attribute%20as%20both%20a%20%3CEM%3E%20Sign-up%20attribute%20%3C%2FEM%3E%20and%20%3CEM%3E%20Application%20claim%20%3C%2FEM%3E%20.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20This%20will%20not%20retroactively%20set%20a%20value%20for%20existing%20users%2C%20so%20when%20testing%20the%20easy%20thing%20to%20do%20is%20to%20delete%20your%20user%20account%20in%20the%20%3CEM%3E%20Users%20%3C%2FEM%3E%20section%2C%20and%20run%20the%20sign%20up%20again.%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74294i341A219EB8D27EB0%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Notice%20how%20there%20is%20a%20new%20textbox%20there%3F%20Type%20in%20%22Partner%22%20and%20create%20the%20user.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Since%20this%20is%20a%20custom%20attribute%20the%20name%20in%20the%20claim%20will%20be%20%3CEM%3E%20extension_Role%20%3C%2FEM%3E%20since%20%3CEM%3E%20extension_%20%3C%2FEM%3E%20will%20be%20applied%20as%20prefix%20to%20claims%20that%20are%20not%20built-in.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Go%20back%20to%20your%20code%20and%20create%20the%20following%20policy%20in%20%3CEM%3E%20Startup.cs%20%3C%2FEM%3E%20%3A%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)%20%3CBR%20%2F%3E%20.AddAzureADB2C(options%20%3D%26gt%3B%20Configuration.Bind(%22AzureADB2C%22%2C%20options))%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20services.AddAuthorization(options%20%3D%26gt%3B%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20options.AddPolicy(%22Partner%22%2C%20policy%20%3D%26gt%3B%20policy.RequireClaim(%22extension_Role%22%2C%20%22Partner%22))%3B%20%3CBR%20%2F%3E%20%7D)%3B%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20In%20%3CEM%3E%20HomeController.cs%20%3C%2FEM%3E%20make%20sure%20the%20Partner%20method%20looks%20like%20this%3A%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20%5BAuthorize(Policy%20%3D%22Partner%22)%5D%20%3CBR%20%2F%3E%20public%20IActionResult%20Partner()%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20ViewData%5B%22Message%22%5D%20%3D%20%22Howdy%20partner!%22%3B%20%3CBR%20%2F%3E%20return%20View()%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Do%20the%20same%20for%20%22Customer%22%20and%20%22Employee%22%20as%20well%2C%20and%20you%20have%20a%20working%20setup%20for%20handling%20different%20access%20levels%20to%20your%20web%20app.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20This%20still%20leaves%20some%20open%20questions%3A%20%3CBR%20%2F%3E%20-%20We%20still%20only%20support%20one%20identity%20provider.%20%3CBR%20%2F%3E%20-%20Should%20we%20really%20have%20users%20typing%20in%20the%20role%20they%20want%20to%20have%20during%20registration%3F%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Yeah%2C%20that%20might%20not%20be%20how%20we%20actually%20want%20it%20to%20be.%20First%20task%20to%20fix%20this%20would%20be%20registering%20more%20identity%20providers.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CSTRONG%3EAdding%20Identity%20Providers%20(IdPs)%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20How%20about%20we%20offer%20local%20and%20social%20accounts%20for%20consumers%2C%20Azure%20AD%20for%20employees%2C%20and%20all%20types%20for%20partners.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Azure%20AD%20for%20employees%20can%20be%20configured%20by%20adding%20an%20OIDC-based%20provider%3A%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Factive-directory-b2c-setup-oidc-azure-active-directory%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Factive-directory-b2c-setup-oidc-azure-active-directory%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F74296i4A2B3D339D0F8967%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20For%20customers%20I%20chose%20to%20add%20Google%20and%20Microsoft%2C%20but%20of%20course%20you%20can%20register%20others%20too%20if%20you%20like.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20partner%20logins%20are%20a%20different%20beast%20to%20tackle.%20Sure%2C%20we%20have%20covered%20social%20identities%20already%2C%20but%20what%20about%20Azure%20AD%3F%20We%20have%20already%20added%20one%20tenant%2C%20but%20we%20definitely%20don't%20want%20to%20add%20all%20our%20partner's%20tenants%20manually.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Azure%20AD%20exposes%20what%20we%20call%20a%20common%20endpoint%20that%20is%20used%20for%20such%20purposes.%20The%20metadata%20url%20for%20this%20is%20%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2Fcommon%2F.well-known%2Fopenid-configuration%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Flogin.microsoftonline.com%2Fcommon%2F.well-known%2Fopenid-configuration%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Which%20would%20make%20you%20think%20that%20-%20let's%20just%20add%20that%20and%20we%20are%20good.%20Well%2C%20we%20are%20not%2C%20since%20the%20UI%20quite%20sternly%20informs%20us%20that%20this%20is%20not%20an%20allowed%20endpoint.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20For%20readers%20familiar%20with%20Azure%20AD%20it%20would%20be%20tempting%20to%20suggest%20that%20we%20should%20attempt%20the%20v2%20endpoint%3A%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fdevelop%2Factive-directory-appmodel-v2-overview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fdevelop%2Factive-directory-appmodel-v2-overview%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20I'm%20not%20going%20to%20dive%20into%20all%20the%20details%20and%20derail%20what%20we%20are%20doing%2C%20but%20I%20tried%2C%20and%20even%20though%20it%20is%20possible%20to%20add%20the%20endpoint%20I%20can't%20actually%20get%20it%20working.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20A%20little%20digging%20in%20the%20docs%20might%20lead%20you%20to%20this%20article%20which%20outlines%20the%20steps%20for%20this%20provider%3A%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Factive-directory-b2c-setup-commonaad-custom%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory-b2c%2Factive-directory-b2c-setup-commonaad-custom%20%3C%2FA%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20conclusion%20of%20this%20specific%20task%20is%20two-fold%3A%20%3CBR%20%2F%3E%20Bad%20news%20-%20we%20cannot%20add%20the%20multi-tenant%20Azure%20AD%20component%20in%20the%20nice%20UI%20we%20have%20used%20so%20far.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Good%20news%20-%20it%20is%20possible%20to%20solve%20it%20through%20other%20means.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Those%20other%20means%20are%20called%20custom%20policies%2C%20and%20since%20they%20are%20a%20mouthful%20we'll%20take%20a%20break%20until%20next%20week%20before%20we%20tackle%20those.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20When%20part%202%20comes%20along%20code%20will%20go%20up%20on%20GitHub%20-%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fahelland%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fgithub.com%2Fahelland%3C%2FA%3E%3C%2FAZUREADB2COPTIONS%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-337482%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Sep%2003%2C%202018%20Authored%20by%20Andreas%20HellandSoftware%20developers%20have%20been%20dealing%20with%20the%20concept%20of%20users%20in%20their%20apps%20for%20many%20years%2C%20and%20many%20of%20us%20have%20implemented%20simple%20schemes%20for%20computing%20a%20hash%20of%20the%20password%20and%20rolling%20our%20own%20mechanisms%20and%20identity%20stores%20to%20support%20this.%3C%2FLINGO-TEASER%3E%3C%2FLINGO-BODY%3E
Senior Member
First published on MSDN on Sep 03, 2018

Authored by Andreas Helland


Software developers have been dealing with the concept of users in their apps for many years, and many of us have implemented simple schemes for computing a hash of the password and rolling our own mechanisms and identity stores to support this. I don't think I have seen anyone recommend as a best practice that you should build your own identity system, and yet this is what people have been doing. Not saying it is impossible to do so, but even though you know what SQL injection is and have guarded your id database against those attacks there are probably a range of other vulnerabilities you aren't able to keep on top of unless you have more resources available to you than the average developer.

On the bright side, I have observed a push towards using Azure AD as the identity system many places in the past couple of years. Of course - this could be just me and the people/companies I run into. But in general people want SSO which requires some centralized component to be involved, and for corporate use Active Directory and/or Azure Active Directory frequently comes into play.

The basic single Azure AD tenant authentication scenario is something most developers should be able to implement fairly easily based on the samples provided by Microsoft. Call it the happy path scenario if you will. Unfortunately a lot of the use cases out there are not the basic single tenant setup. Maybe you want multi-tenant support. Maybe you need support for social identities like Facebook and Google. And maybe you want to provide different frontends for consumers, partners, etc. That's when things start to get more complicated :)

Even though I have my Microsoft goggles on I have no problem admitting there are great tools out there, made by other companies, for managing identities and securing resources. However since I have invested time and effort into the Azure technology stack it makes sense for me to try to implement this with MS tech, so I thought I would take a stab at building out my own sample using Azure AD B2C for Identity as a Service (IdaaS).

I have done a couple of posts on AAD B2C before, and of course you will also find info from Microsoft themselves on what Azure AD B2C is so I'm not going to do an introductory part on what the service can do. The name of the service does it no favors though since it seems to imply it's all about consumers. The identity teams in Redmond are now more often referring to "external identities" which is a better term in my opinion. (I would love to see it being a more generic "Azure AD Dev Edition" without a distinction between B2C and non-B2C, but it's not quite there yet. And maybe it needn't be either; that is however an entirely different discussion we will not go into now.)

If you want to learn the more basic parts of AAD B2C, and for that sake some more advanced things too, Microsoft has a thorough ebook you can go through: https://aka.ms/learnAADB2C

And thus there is no need for further explanations right?


Thing is - before you start hacking the necessary pieces of code into your solution you need a strategy of sorts so that is where we will start.

Which identity providers (IdPs) will you support?
Do you want to support primarily social identities like Facebook, Google, etc. or do you want to primarly support corporate identities? Or a mix? Only Azure AD tenants or other corporate-based ones as well?

The adding of IdPs is entirely independent of your code, so that should be done in the AAD B2C portal before you start. You don't need to add all at once, but with none defined things are not going to work so add at least one.

Google: https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-setup-goog-app ...
Facebook: https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-setup-fb-app
Microsoft: https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-setup-msa-app ...

For adding corporate identities I would recommend using the OpenID Connect option if you can. Both Azure AD and ADFS can be added this way, and it is easier than using custom policies. (More on that topic later on.)

Custom OIDC: https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-setup-oidc-idp ...

The third of the major options is local accounts - these are identities defined locally in Azure AD B2C and provisioned by the AAD B2C service natively. Some users will want to silo their different logins for various reasons. Should you cater for these users as well? Personally I like having this option, but this is a design point you need to consider specifically for your use case.

Technically speaking the local accounts are also AAD accounts, but in a B2C version. In a regular AAD tenant users are assumed to be working for the same company - this could of course include external consultants as well, but users are aware of each other and who the company entity owning the identities is. This means that you have things like a Global Address List (GAL) where you can look up the phone number of other users and similar things. In an system where users are all external and independent of each other this would not be ok, and would probably violate several privacy laws, so there are restrictions in place backend that prevents one user from looking up the details of another user.

Being AAD accounts means you as the tenant owner and app developer can still use things like the Graph API to query the records and other actions, so it's still better than rolling your own database for local accounts. (As a side note - the MS Graph is not supported yet, and you will need to use the Azure AD Graph.)

How will the landing page of the SignUp and/or SignIn experience be?
Will you leave the choice of identity entirely up to the user, or will you guide (with more or less force), towards specific options?

For instance you could have a page asking "Are you a partner or consumer" and direct partners towards corporate identities whereas consumers will only get to choose between different social identities. At the same time it could be you have one-man shops as partners that aren't using Office 365 and Azure AD, and prefer a Google account instead.

You could also have an automated flow once you know the email address of the user through a process called home realm discovery. In this case you could have users type in their email address first, and if it's joe@contoso.com send them directly to the Contoso login page. Do keep in mind to that there are some open-ended scenarios - just because an email address ends in gmail.com doesn't mean the identity has to be a Google account.

What needs to be customized, and what will work out of the box?
This is somewhat AAD B2C-specific since custom policies are an important part of achieving more advanced scenarios. You could however argue that some of the considerations would be valid even without AAD B2C. More specialized use cases will require more custom work with or without AAD B2C.

Take something like resetting passwords. This is a fairly common use case on web pages that rely on logging in. But if the user logs in with Facebook it's not like you can perform a reset - that would be something Facebook would need to provide. If you enable local accounts in AAD B2C you are responsible for that experience. Sure, you would "outsource" the task to Azure AD, but you would need to account for it in your UI. Luckily this is a simple feature that can be implemented in a standard policy in B2C, and as long as you send the user to the right page in the UI you might not need all the extras custom policies bring to the table.

This brings out a key point with Azure AD B2C - if the standard template provides what you need you should not build a custom policy "just because you can".

What would be a valid use case for a custom policy then? Maybe you need to validate some of the inputs the user types in when signing up and that validation needs to be done through an API call. That would require a custom policy. Maybe the user needs a code you have provided in advance to be able to sign up. That would require a custom policy. Basically, if you don't find it in the UI in the Azure Portal you need to think custom. Let's be realistic though - there are of course limits to what you can do in the custom space as well. You can do more, but you can't do everything :)

Authorization
Authentication is the easy part. Well, easy might be stretching it; there are as you can see plenty of considerations on how to acquire your golden ticket (aka token). But once the user has a token then what? If you have a setup with partners and end-users it's probably fair to assume that they should not be able to access the same content. You will probably need to have different roles, and use that as a filtering mechanism. Plan this out before you hack along. (Figuring out details like whether you need two roles on the partner level, say "Partner Admin" and "Partner User", isn't needed. You can do that later on.)

Our plan
With those considerations out of the way let's spec out what we want to implement.

We will build a web app that will provide services to employees, partners, and end-users/customers. For this we will support both Azure AD, social accounts, and local accounts. The web app will be implemented in .NET Core 2.1.

I will admit that I was inspired by a Microsoft demo:
https://github.com/Azure-Samples/active-directory-external-identities-woodgrove-demo

I don't intend to copy it outright though, fact is I didn't copy any of the source, and I will deliberately create a less polished version with regards to UI to focus on specific parts.

We will place as much of the authentication and authorization logic in AAD B2C as we can keeping the middleware configuration clean.

The first step is to create a new project in Visual Studio 2017 using the ASP.NET Core Web Application template.



Notice that authentication is set to No Authentication as we will build in that manually. Also make sure that Configure for HTTPS is checked - this is needed for authentication services.

As always, make sure the basics are working by hitting F5 and verify that the app builds and deploys without issues.

Before producing more code we will head over the Azure AD B2C Portal to create a few pieces and make sure things are working there.

Azure AD B2C Basics
If you don't have an AAD B2C tenant already that would be the first step. Microsoft details this here:
https://docs.microsoft.com/en-us/azure/active-directory-b2c/tutorial-create-tenant

Make sure that you have Local accounts listed in the Identity providers section:


The tooltip explains that you can choose between Email and Username for these accounts - I will go with email.

Let's create a very basic set of policies. We just want to make sure that we can sign up and sign in first. Move down to Sign-up policies and click Add :


Check Email signup in the Identity providers section:


Sign-up attributes are the parameters we want to collect from the user when they create their account. You can check every one of them if you like, but that means more typing when testing :)


Application claims are the parameters you want to be part of the token you issue when someone signs in:


What is the difference? Well, some attributes does not make sense to collect from the user, but makes sense for the app. Other attributes are only relevant to have on file backend. The sign up attributes get persisted to the user object, but you can also have ephemeral attributes that only live in the context of a session.

You can add additional custom attributes if you like, but you're still somewhat limited until you go for custom polices. For now we don't need any extras.

We don't need Multifactor authentication or Page UI customization for now either so hit Create on the bottom of the page.

To use policies, or B2C in any way, you need to create an application. Move to Applications and click Add .

Check Yes to it being a Web App / Web API, and use https://jwt.ms as the reply url.


Go back to the Sign-up policies section and click the policy you created a couple of moments ago. Select the application you just created, and make sure jwt.ms is the reply url before clicking the Run now button.

Now you get to sign up for an account in your tenant:


After creating the account you're automatically redirected to a page that shows you your token and the contents of it. (When you troubleshoot policies you will appreciate this feature.)


Wasn't that easy? You're creating identities and testing them without a single line of code written!

Next you should create a Sign-in policy , and a Password reset policy . I made the Sign-in more or less similar to Sign-up. With the password reset you don't really have that many options. Note that password reset only works with local accounts.

Actually, let's pull one more trick with the AAD B2C built-in policies. Head to Sign-up or sign-in policies and fill the forms out the same way. A "SuSi" policy produces a screen where the user chooses if they want to sign up or sign in thus sparing you a couple of calories as a coder when you're implementing your login links.

Configuring authentication in your web app
Now we can try to implement this in our own app. Go back to the app you registered in the B2C Portal. Copy the application id to Notepad or something.

We also need to register our reply url for our app - so check with port your app runs on:


Mine is running on port 44315 as you can see. So register https://localhost:44315/signin-oidc as a reply url:


What I didn't show you when we created the web app was that there is actually a wizard very suited for Azure AD B2C in Visual Studio 2017:


However you wouldn't be able to actually fill it out had you not gone through the AAD B2C portal first. Let's add the code that would have been generated the manual way.

First you need to add the following NuGet package:
Microsoft.AspNetCore.Authentication.AzureADB2C.UI

The authentication is configured in Startup.cs .

Add two usings:
[code language="csharp"]
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.AzureADB2C.UI;
[/code]

Add the following to the ConfigureServices method:
[code language="csharp"]
services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
.AddAzureADB2C(options => Configuration.Bind("AzureADB2C", options));
[/code]

Add one line to the Configure method:
[code language="csharp"]
app.UseAuthentication()
[/code]

Time to add the settings for our AAD B2C tenant - these can be found in appsettings.json .
[code language="csharp"]
"AzureAdB2C": {
"Instance": "https://login.microsoftonline.com/tfp/",
"ClientId": "applicationId-from-portal",
"CallbackPath": "/signin-oidc",
"Domain": "yourtenant.onmicrosoft.com",
"SignUpSignInPolicyId": "B2C_1_ContosoLocalSuSi",
"ResetPasswordPolicyId": "B2C_1_ContosoLocalPasswordReset",
"EditProfilePolicyId": ""
},
[/code]

We also need a UI with a button for signing in.

Add a file called _LoginPartial.cshtml with the following contents:
[code language="csharp"]
@using System.Security.Principal
@using Microsoft.AspNetCore.Authentication.AzureADB2C.UI
@using Microsoft.Extensions.Options
@inject IOptionsMonitor<AzureADB2COptions> AzureADB2COptions

@{
var options = AzureADB2COptions.Get(AzureADB2CDefaults.AuthenticationScheme);
}

@if (User.Identity.IsAuthenticated)
{
<ul class="nav navbar-nav navbar-right">
@if (!string.IsNullOrEmpty(options.EditProfilePolicyId))
{
<li><a asp-area="AzureADB2C" asp-controller="Account" asp-action="EditProfile">Hello @User.Identity.Name!</a></li>
}
else
{
<li class="navbar-text">Hello @User.Identity.Name!</li>
}
<li><a asp-area="AzureADB2C" asp-controller="Account" asp-action="SignOut">Sign out</a></li>
</ul>
}
else
{
<ul class="nav navbar-nav navbar-right">
<li><a asp-area="AzureADB2C" asp-controller="Account" asp-action="SignIn">Sign in</a></li>
</ul>
}
[/code]

Add a partial to _Layout.cshtml like this:
[code language="csharp"]
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>
</ul>
<partial name="_LoginPartial" />
</div>
[/code]

If you hit F5 now you will see a Sign In button, and clicking it will take you to AAD B2C and return you to your web site afterwards. Nice and simple.

Hold on, it just says "Hello !" - there is no name there. Yes, due to how I configured my policies it doesn't line up with the claims expected to print out properly. This is purely a cosmetic issue that we will not care about right now. You are logged in.

In the web app as it is now this sign in action does not serve any purpose. You can browse everything whether you're authenticated or not.

While it is a silly example let's say only authenticated users can view our contact info.

Configuring authorization in your web app
Open up HomeController.cs and add an Authorize attribute to your Contact action:
[code language="csharp"]
[Authorize]
public IActionResult Contact()
{
...
}
[/code]

And add using Microsoft.AspNetCore.Authorization; along with the other usings.

If you try to click Contact without being logged in you will be taken to the sign-in page to type in your credentials.

Ok, so now we have a very basic model for authentication (AuthN) and authorization (AuthZ) in our web app. We are however still a bit off with regards to our original plan - what now?

I'll add views for partners, customers and employees to my app.
Add methods in HomeController.cs :
[code language="csharp"]
public IActionResult Partner()
{
ViewData["Message"] = "Howdy partner!";
return View();
}

public IActionResult Customer()
{
ViewData["Message"] = "Welcome dear customer. How may we help you today.";
return View();
}

public IActionResult Employee()
{
ViewData["Message"] = "Get back to work!";
return View();
}
[/code]

I use scaffolding to create views (right-click method, select Add View , and use the Empty template.)

Add a few menu items to _Layout.cshtml :
[code language="csharp"]
<ul class="nav navbar-nav">
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Partner">Partner</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Customer">Customer</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Employee">Employee</a></li>
</ul>
[/code]

How to make sure only partners can access the partner page and so on?

We will use .NET Core Role-based authorization:
https://docs.microsoft.com/en-us/aspnet/core/security/authorization/roles?view=aspnetcore-2.1

This approach will work, in the sense that you will get access denied even if you sign in. Having role checks doesn't work if you do not have a way to get users into those roles which sort of breaks the basic setup…

We're using claims-based authentication though, and have claims we are supposed to be able to check - so maybe we can do something with those?
Probably: https://docs.microsoft.com/en-us/aspnet/core/security/authorization/claims?view=aspnetcore-2.1 ...

If we can add the right claim we can make a policy for it. There doesn't seem to be a ready-made attribute for this, but luckily AAD B2C lets us customize this with little effort by adding custom attributes so go back to the AAD B2C Portal.

Navigate to the User attributes section and click Add . Add an attribute called Role :


Edit your policy to include this attribute as both a Sign-up attribute and Application claim .

This will not retroactively set a value for existing users, so when testing the easy thing to do is to delete your user account in the Users section, and run the sign up again.


Notice how there is a new textbox there? Type in "Partner" and create the user.

Since this is a custom attribute the name in the claim will be extension_Role since extension_ will be applied as prefix to claims that are not built-in.

Go back to your code and create the following policy in Startup.cs :
[code language="csharp"]
services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
.AddAzureADB2C(options => Configuration.Bind("AzureADB2C", options));

services.AddAuthorization(options =>
{
options.AddPolicy("Partner", policy => policy.RequireClaim("extension_Role", "Partner"));
});
[/code]

In HomeController.cs make sure the Partner method looks like this:
[code language="csharp"]
[Authorize(Policy ="Partner")]
public IActionResult Partner()
{
ViewData["Message"] = "Howdy partner!";
return View();
}
[/code]

Do the same for "Customer" and "Employee" as well, and you have a working setup for handling different access levels to your web app.

This still leaves some open questions:
- We still only support one identity provider.
- Should we really have users typing in the role they want to have during registration?

Yeah, that might not be how we actually want it to be. First task to fix this would be registering more identity providers.

Adding Identity Providers (IdPs)
How about we offer local and social accounts for consumers, Azure AD for employees, and all types for partners.

Azure AD for employees can be configured by adding an OIDC-based provider:
https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-setup-oidc-azur...



For customers I chose to add Google and Microsoft, but of course you can register others too if you like.

The partner logins are a different beast to tackle. Sure, we have covered social identities already, but what about Azure AD? We have already added one tenant, but we definitely don't want to add all our partner's tenants manually.

Azure AD exposes what we call a common endpoint that is used for such purposes. The metadata url for this is https://login.microsoftonline.com/common/.well-known/openid-configuration

Which would make you think that - let's just add that and we are good. Well, we are not, since the UI quite sternly informs us that this is not an allowed endpoint.

For readers familiar with Azure AD it would be tempting to suggest that we should attempt the v2 endpoint:
https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-appmodel-v2-ove...

I'm not going to dive into all the details and derail what we are doing, but I tried, and even though it is possible to add the endpoint I can't actually get it working.

A little digging in the docs might lead you to this article which outlines the steps for this provider:
https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-setup-commonaad...

The conclusion of this specific task is two-fold:
Bad news - we cannot add the multi-tenant Azure AD component in the nice UI we have used so far.

Good news - it is possible to solve it through other means.

Those other means are called custom policies, and since they are a mouthful we'll take a break until next week before we tackle those.

When part 2 comes along code will go up on GitHub - https://github.com/ahelland