1

L'intégration de l'application MVC avec l'API Web, Azure Users Authentication est effectuée à l'aide de OWIN, Vous souhaitez supprimer le cookie d'authentification et transmettre le jeton dans l'en-tête. comment faire? J'utilise le fichier MSAL.cs pour l'authentification Azure AD. Voulez-vous passer le jeton dans l'en-tête d'appel api. premier chargement de la page d'application MVC, après l'authentification, appelez les méthodes web api. je code suivant pour autherization AD d'azur,Passer le jeton dans l'en-tête pour l'authentification dans MVC et Web API

private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification notification) 
      { 
       // Extract the code from the response notification 
       var code = notification.Code; 

       string signedInUserID = notification.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value; 
       TokenCache userTokenCache = new MSALSessionCache(signedInUserID, notification.OwinContext.Environment["System.Web.HttpContextBase"] as HttpContextBase).GetMsalCacheInstance(); 
       ConfidentialClientApplication cca = new ConfidentialClientApplication(ClientId, Authority, RedirectUri, new ClientCredential(ClientSecret), userTokenCache, null); 
       try 
       { 
        AuthenticationResult result = await cca.AcquireTokenByAuthorizationCodeAsync(code, Scopes); 
       } 
       catch (Exception ex) 
       { 
        //TODO: Handle 
        throw; 
       } 
      } 
+3

Bienvenue dans StackOverflow. Veuillez fournir un [exemple minimal, complet et vérifiable] (https://stackoverflow.com/help/mcve) ou cette question sera probablement fermée. Veuillez inclure les détails pertinents (les noms des cookies et des en-têtes, par exemple) dans la question, ainsi que ce que vous avez essayé jusqu'à présent. – NightOwl888

Répondre

0

Après le premier signe de temps les utilisateurs d'annonces d'azur à l'aide du middleware ASP.Net OpenID Connect OWIN, si vous voulez appeler api web, vous pouvez ajouter le jeton à en-tête de la demande:

string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value; 
string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; 
string authority = String.Format(CultureInfo.InvariantCulture, Startup.aadInstance, tenantID, string.Empty); 
ClientCredential credential = new ClientCredential(Startup.clientSecret); 

// Here you ask for a token using the web app's clientId as the scope, since the web app and service share the same clientId. 
app = new ConfidentialClientApplication(Startup.clientId, redirectUri, credential, new NaiveSessionCache(userObjectID, this.HttpContext)){}; 
result = await app.AcquireTokenSilentAsync(new string[] { Startup.clientId }); 

HttpClient client = new HttpClient(); 
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, serviceUrl + "/api/todolist"); 
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.Token); 

HttpResponseMessage response = await client.SendAsync(request); 

S'il vous plaît se référer à code sample pour plus de détails.