اتبع: https://github.com/azuread/microsoft-Intity-web لنهج أفضل ودعم على أساس MSAL.
الغرض من هذه المكتبة هو تبسيط التفاعل مع مكتبات المصادقة في Microsoft والاتصال بالجهة الأولى وواجهة برمجة التطبيقات الأخرى.
يرجى الاطلاع على أن هذا لا يزال يعمل قيد التقدم ، وهناك أشياء مثل حماية البيانات في ذاكرة التخزين المؤقت وما إلى ذلك.
// 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 AD والمجموعات أكثر بساطة.
بفضل هذا الامتداد ، يمكنك الاستفادة من التفويض في الوقت الفعلي والدور القائم على الدور. حاليا ، الإعداد هو أنه يجب تلبية أي من المتطلبات. إذا كنت تريد متعددة ، مثل عضوية المجموعة والدور ، فما عليك سوى تكديسها فوق بعضها البعض.
لكي يعمل هذا ، تحتاج أيضًا إلى إضافة 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 ) ;
}
} لكي ينجح هذا ، يجب أن يكون لديك JWTbearermDdleware Setup بشكل صحيح (مع SaveTokens = true ) لإعلان Azure في مشروعك. تعمل هذه الطريقة على تبسيط الخلاص الرمزي للتدفق أثناء الاستفادة من ذاكرة التخزين المؤقت الرمزية وكل شيء.
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 ) ;
}
}