Folgen Sie: https://github.com/azuread/microsoft-identity-web für einen besseren und unterstützten Ansatz basierend auf MSAL.
Der Zweck dieser Bibliothek ist es, die Interaktion mit den Authentifizierungsbibliotheken von Microsoft zu vereinfachen und die 1. Partei und andere APIs anzurufen.
Beachten Sie bitte, dass dies noch in Arbeit ist und es Dinge wie Datenschutz in den Cache usw. gibt. Im Moment fehlt.
// 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 ) ;
}
} Sobald Sie die oben genannten Einrichtungen eingerichtet haben, können Sie auch die AzureAdAuthorizationAttribute verwenden. Der Zweck davon ist, die Genehmigung mit Azure -Anzeigenrollen und Gruppen einfacher zu gestalten.
Dank dieser Erweiterung können Sie Echtzeitgruppen und rollenbasierte Autorisierung verwenden. Derzeit ist das Setup, dass einer der Anforderungen erfüllt werden muss. Wenn Sie mehrere, z. B. Gruppenmitglied und Rolle haben möchten, stapeln Sie sie einfach übereinander.
Damit dies funktioniert, müssen Sie auch IHttpContextAccessor zu Ihren Diensten hinzufügen.
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 ) ;
}
} Damit dies funktioniert, müssen Sie in Ihrem Projekt eine korrekte Einrichtung von JWTBearerMiddleware (mit SaveTokens = true ) für Azure -Anzeigen haben. Diese Methode vereinfacht die Erlösung des Durchflusss vor dem Durchfluss und nutzt den Token-Cache und alles.
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 ) ;
}
}