Ikuti: https://github.com/azuread/microsoft-identity-web untuk pendekatan yang lebih baik dan didukung berdasarkan MSAL.
Tujuan perpustakaan ini adalah untuk menyederhanakan interaksi dengan pustaka otentikasi Microsoft dan memanggil pihak pertama dan API lainnya.
Harap ingat bahwa ini masih dalam proses dan ada hal -hal seperti perlindungan data ke cache dll. Hilang sekarang.
// 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 ) ;
}
} Setelah Anda mengatur yang di atas, Anda juga dapat memanfaatkan AzureAdAuthorizationAttribute . Tujuan dari ini adalah untuk membuat otorisasi dengan peran iklan Azure dan kelompok lebih sederhana.
Berkat ekstensi ini, Anda dapat menggunakan otorisasi kelompok waktu nyata dan peran. Saat ini, pengaturan adalah bahwa salah satu persyaratan harus dipenuhi. Jika Anda menginginkan banyak, seperti keanggotaan dan peran grup, cukup tumpuk satu sama lain.
Agar ini berfungsi, Anda juga perlu menambahkan IHttpContextAccessor ke layanan Anda.
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 ) ;
}
} Agar ini berfungsi, Anda harus memiliki pengaturan jwtbearermiddleware dengan benar (dengan SaveTokens = true ) untuk Azure AD di proyek Anda. Metode ini menyederhanakan penebusan token aliran-di-behalf-of-behalf-of-Behalf-of-Behalf-of-Behalf-of-Behalf-of.
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 ) ;
}
}