GithubHelp home page GithubHelp logo

damienbod / aspnetcoremicrosoftgraph Goto Github PK

View Code? Open in Web Editor NEW
23.0 5.0 9.0 805 KB

ASP.NET Core application and API using Microsoft Graph delegated clients

Home Page: https://damienbod.com/2020/11/20/using-microsoft-graph-api-in-asp-net-core/

License: MIT License

C# 69.65% HTML 24.02% CSS 5.45% JavaScript 0.88%
oauth2 oidc azure identity graph graph-api microsoft-identity-platform

aspnetcoremicrosoftgraph's Introduction

.NET

Using Microsoft Graph API delegated clients in ASP.NET Core

https://damienbod.com/2020/11/20/using-microsoft-graph-api-in-asp-net-core/

Delegated API permissions

Sites.Read.All

Graph API setup in Web API project

builder.Services.AddScoped<GraphApiClientDirect>();

builder.Services.AddMicrosoftIdentityWebApiAuthentication(builder.Configuration)
    .EnableTokenAcquisitionToCallDownstreamApi()
    .AddMicrosoftGraph()
    .AddInMemoryTokenCaches();

appsettings.json

  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "damienbodsharepoint.onmicrosoft.com",
    "CallbackPath": "/signin-oidc",
    "ClientId": "89cbcff9-7c4e-4659-9948-d6f7fda186e1", // sharepoint graph api
    "TenantId": "5698af84-5720-4ff0-bdc3-9d9195314244"
    // Secret is required for the downstream Graph API call
    // secret is not required for the API itself
    //"ClientSecret": "--secret-in-user-secrets--"
  },

Graph service from API for OBO flow, downstream API

private readonly GraphServiceClient _graphServiceClient;

// "user.read Sites.Read.All" consented in the App registration
// The default scope is used because this is a downstream API OBO delegated user flow
private const string SCOPES = "https://graph.microsoft.com/.default";

public GraphApiClientDirect(GraphServiceClient graphServiceClient)
{
   // https://graph.microsoft.com/.default
   // "user.read Sites.Read.All" consented in the App registration
   _graphServiceClient = graphServiceClient;
}

History

  • 2023-12-02 Fix photo streaming
  • 2023-11-22 Updated .NET 8
  • 2023-08-26 Improved code, added comments
  • 2023-08-22 Updated to Graph 5
  • 2023-08-22 Updated packages and startup
  • 2023-03-02 Updated packages
  • 2023-01-15 Updated packages to .NET 7
  • 2022-06-10 Updated packages
  • 2022-01-28 Updated packages
  • 2021-12-15 Updated packages
  • 2021-10-29 Updated to .NET 6, Microsoft.Identity.Web 1.18.0
  • 2021-02-28 Updated to Microsoft.Identity.Web 1.7.0
  • 2021-02-13 Updated to Microsoft.Identity.Web 1.6.0

Links

https://developer.microsoft.com/en-us/graph/

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests

https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient

aspnetcoremicrosoftgraph's People

Contributors

damienbod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

aspnetcoremicrosoftgraph's Issues

Error on missing client secret / certificate

MsalClientException: IDW10104: Both client secret and client certificate cannot be null or whitespace, and only ONE must be included in the configuration of the web app when calling a web API. For instance, in the appsettings.json file.
Microsoft.Identity.Web.MicrosoftIdentityOptionsValidation.ValidateEitherClientCertificateOrClientSecret(string clientSecret, IEnumerable cert)
Microsoft.Identity.Web.TokenAcquisition.BuildConfidentialClientApplicationAsync()
Microsoft.Identity.Web.TokenAcquisition.GetOrBuildConfidentialClientApplicationAsync()
Microsoft.Identity.Web.TokenAcquisition.AddAccountToCacheFromAuthorizationCodeAsync(AuthorizationCodeReceivedContext context, IEnumerable scopes)
Microsoft.Identity.Web.MicrosoftIdentityWebAppAuthenticationBuilder+<>c__DisplayClass11_1+<b__1>d.MoveNext()
Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.RunAuthorizationCodeReceivedEventAsync(OpenIdConnectMessage authorizationResponse, ClaimsPrincipal user, AuthenticationProperties properties, JwtSecurityToken jwt)
Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.HandleRemoteAuthenticateAsync()

Any idea what this could be ? I have passed in the client secret.

"IDW10502: An MsalUiRequiredException was thrown due to a challenge for the user

I am using React with Asp.net core web api.
I have implemented as explained.
In React, I am using scope as openid and the access as user where the same implemented in the sample UI.
But I am getting the following exception. Can you please help me on that issue?

"IDW10502: An MsalUiRequiredException was thrown due to a challenge for the user

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.