Giter VIP home page Giter VIP logo

msal-interactive-netcore's Introduction

services platforms author level client service endpoint
active-directory
dotnet
bogavril
200
.NET Core Console
Microsoft Graph
AAD V2

This sample is for MSAL 3.x and previous MSAL versions do not support it.

.NET Core application signing in users interactively (Windows, Linux, Mac) with Microsoft and calling the Microsoft Graph

This simple sample demonstrates how to use the Microsoft Authentication Library (MSAL) for .NET to get an access token and call the Microsoft Graph (using OAuth 2.0 against the Azure AD v2.0 endpoint).

This sample is pre-configured. If you just want to quickly run it just:

  1. Clone the code.
  git clone https://github.com/Azure-Samples/active-directory-dotnet-desktop-msgraph-v2.git
  1. Build and run using the dotnet command line or using Visual Studio or Visual Studio Code

Notes about the user experience

On most platforms, MSAL is able to pop-up a browser by hosting it in an UI panel. .NET core does not have an UI controls. This solution uses the OS default browser, which is started as a separate process. Implications of this are:

  • MSAL does not have control over the browser, e.g. MSAL cannot close the window, cannot detect if the user decides to navigate away etc. The app using MSAL can only set a cancellation token / timeout.
  • On succesful navigation to the redirect URI, the application can write a message back to the browser. The experience can be futher enhanced by redirecting the browser to a page of your choice.
  • In order to capture the result of the authentication, MSAL listens to a localhost socket. Applications must register "http:\localhost" as a redirect uri.
  • B2C does not support localhost redirect uri.

[Optional] Use your own application coordinates

If you want to use your own application coordinates, please follow these instructions:

Choose the Azure AD tenant where you want to create your applications

  1. Sign in to the Azure portal using either a work or school account or a personal Microsoft account.
  2. If your account gives you access to more than one tenant, select your account in the top right corner, and set your portal session to the desired Azure AD tenant (using Switch Directory).
  3. In the left-hand navigation pane, select the Azure Active Directory service, and then select App registrations (Preview).

Register the client app

  1. Navigate to the Microsoft identity platform for developers App registrations page.
  2. Select New registration.
    • In the Name section, enter a meaningful application name that will be displayed to users of the app, for example NetCoreApp.
    • In the Supported account types section, select Accounts in any organizational directory and personal Microsoft accounts (e.g. Skype, Xbox, Outlook.com).
    • Select Register to create the application.
  3. On the app Overview page, find the Application (client) ID value and record it for later. You'll need it to configure the Visual Studio configuration file for this project.
  4. In the list of pages for the app, select Authentication.
    • In the Redirect URLs, add a new Public client (mobile & desktop) uri to http://localhost
  5. Select Save.
  6. Configure Permissions for your application. To that extent in the list of pages click on API permissions
    • click the Add a permission button and then,
    • Ensure that the Microsoft APIs tab is selected
    • In the Commonly used Microsoft APIs section, click on Microsoft Graph
    • In the Delegated permissions section, ensure that the right permissions are checked: User.Read. Use the search box if necessary.

Configure the code to use your application's coordinates

Open Program.cs and replace the ClientID string constant with the application / client ID for the app you just registred

System diagram

flow of data

msal-interactive-netcore's People

Contributors

bgavrilms avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

henrik-me

msal-interactive-netcore's Issues

ReadAsync Hangs from IE and Edge

This is a great concept and I thank you for sharing the code. But I want to let you know that for me and another co-working, it does not work the majority of the time when using Internet Explorer or Edge. For the other browsers, it works 100% of the time.

Tracking down the problem, we can clearly see that the call to await networkStream.ReadAsync on line 137 of SingleMessageTcpListener.cs will hang until the browser times out, at which point the code will resume, but doesn't read any bytes.

We have reproduced the problem on three Windows 10 machines, each happen to be Windows 10 Enterprise 1809, but slightly different builds:

  • 17763.292
  • 17763.437
  • 17763.503

As far as I know, the Edge browser version on each is 44.17763.1.0 with EdgeHtml version of 18.17763.

And Internet Explorer 11.253.17763.0 Update version 11.0.105.

Note, there have been a couple of times where it was successful, but for me and my co-worker, the overwhelming amount of times, it fails/hangs. If you cannot reproduce it, try things like completely closing the browser, clearing your cache, trying it back-to-back several times in a row, trying it with Edge and Internet Explorer.

Also note that it seems every time I run Fiddler, with it capturing, it will work. I then stop capturing and close Fiddler and often it will continue to work. After a while, I close Edge and clear all of the cache, and try again and it then fails/hangs again, as described above.

Another test I tried was using Internet Explorer 11 on a Windows 7 VM. So far it has worked every time with that.

If you'd like, I'd be happy to record a video and post it or send it directly to you.

Thanks.

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.