ติดตาม: https://github.com/azuread/microsoft-identity-web สำหรับวิธีการที่ดีกว่าและสนับสนุนตาม MSAL
จุดประสงค์ของไลบรารีนี้คือการทำให้การโต้ตอบกับไลบรารีการตรวจสอบความถูกต้องของ Microsoft ง่ายขึ้นและโทรหาบุคคลที่ 1 และ API อื่น ๆ
โปรดทราบว่าสิ่งนี้ยังคงทำงานอยู่และมีสิ่งต่าง ๆ เช่นการป้องกันข้อมูลลงในแคช ฯลฯ หายไปในขณะนี้
// This library depends on MemoryCache / DistributedMemorycache for storing Tokens from the TokenCache
services . AddMemoryCache ( ) ;
services . AddSingleton ( new ClientCredential ( _config [ "AzureAd:ClientId" ] , _config [ "AzureAd:ClientSecret" ] ) ) ;
services . AddScoped < TokenCacheFactory > ( ) ;
services . AddScoped < AdalFactory > ( ) ;
services . AddScoped < MicrosoftGraphFactory > ( ) ;
services . AddScoped < AzureAdGraphFactory > ( ) ; services . Configure < OpenIdConnectOptions > ( AzureADDefaults . OpenIdScheme , options =>
{
options . ResponseType = OpenIdConnectResponseType . CodeIdToken ;
options . Events = new OpenIdConnectEvents ( )
{
OnAuthorizationCodeReceived = async context =>
{
var authContext = context . HttpContext . RequestServices . GetRequiredService < AdalFactory > ( ) . GetAuthenticationContextForUser ( context . Principal ) ;
var clientCred = context . HttpContext . RequestServices . GetRequiredService < Microsoft . IdentityModel . Clients . ActiveDirectory . ClientCredential > ( ) ;
var authResult = await authContext . AcquireTokenByAuthorizationCodeAsync ( context . ProtocolMessage . Code , new Uri ( context . Properties . Items [ OpenIdConnectDefaults . RedirectUriForCodePropertiesKey ] ) , clientCred , "https://graph.microsoft.com" ) ;
context . HandleCodeRedemption ( authResult . AccessToken , authResult . IdToken ) ;
} ,
} ;
} ) ; public class HomeController : Controller
{
private readonly MicrosoftGraphFactory _graphFactory ;
public HomeController ( MicrosoftGraphFactory graphFactory )
{
_graphFactory = graphFactory ;
}
[ Authorize ]
public async Task < IActionResult > Index ( )
{
var graphClient = _graphFactory . GetClientForUser ( HttpContext . User ) ;
var users = await graphClient . Users . Request ( ) . GetAsync ( ) ;
return Json ( users ) ;
}
public async Task < IActionResult > IndexAsApp ( )
{
var tenantId = "" ;
var graphClient = _graphFactory . GetClientForApplication ( tenantId ) ;
var users = await graphClient . Users . Request ( ) . GetAsync ( ) ;
return Json ( users ) ;
}
} เมื่อคุณตั้งค่าข้างต้นแล้วคุณอาจใช้ประโยชน์จาก AzureAdAuthorizationAttribute จุดประสงค์ของสิ่งนี้คือเพื่อให้การอนุญาตด้วยบทบาทโฆษณา Azure และกลุ่มง่ายขึ้น
ด้วยส่วนขยายนี้คุณสามารถใช้ประโยชน์จากกลุ่มเรียลไทม์และการอนุญาตตามบทบาท ขณะนี้การตั้งค่าคือต้องปฏิบัติตามข้อกำหนดอย่างใดอย่างหนึ่ง หากคุณต้องการหลาย ๆ อย่างเช่นการเป็นสมาชิกกลุ่มและบทบาทเพียงแค่ซ้อนมันอยู่ด้านบนของกันและกัน
สำหรับสิ่งนี้ในการทำงานคุณต้องเพิ่ม IHttpContextAccessor ลงในบริการของคุณ
public void ConfigureServices ( IServiceCollection services )
{
.. .
services . AddHttpContextAccessor ( ) ;
.. .
} public class HomeController : Controller
{
[ AzureAdAuthorization ( roles : new string [ ] { AzureAdRoles . CompanyAdministrator } , groups : new string [ ] { ApplicationGroupIds . AppAdministrators } ) ]
public async Task < IActionResult > Index ( )
{
var graphClient = _graphFactory . GetClientForUser ( HttpContext . User ) ;
var users = await graphClient . Users . Request ( ) . GetAsync ( ) ;
return Json ( users ) ;
}
} เพื่อให้สิ่งนี้ใช้งานได้คุณจะต้องตั้งค่า JWTBearerMiddleware อย่างถูกต้อง (พร้อม SaveTokens = true ) สำหรับ Azure AD ในโครงการของคุณ วิธีนี้ช่วยลดความซับซ้อนของการไถ่โทเค็นในการไหลของการไหลในขณะที่ใช้ประโยชน์จากแคชโทเค็นและทุกอย่าง
public class HomeController : Controller
{
private readonly MicrosoftGraphFactory _graphFactory ;
public HomeController ( MicrosoftGraphFactory graphFactory )
{
_graphFactory = graphFactory ;
}
[ Authorize ( JwtBearerDefaults . AuthenticationScheme ) ]
public async Task < IActionResult > Index ( )
{
var graphClient = _graphFactory . GetClientForApiUser ( HttpContext . GetTokenAsync ( "access_token" ) , HttpContext . User ) ;
var users = await graphClient . Users . Request ( ) . GetAsync ( ) ;
return Json ( users ) ;
}
}