Skip to content

Latest commit

 

History

History

user-auth

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

How to run the completed project

Prerequisites

To run the completed project in this folder, you need the following:

  • Go installed on your development machine. (Note: This tutorial was written with Go version 1.19.3. The steps in this guide may work with other versions, but that has not been tested.)
  • A Microsoft work or school account.

If you don't have a Microsoft account, you can sign up for the Microsoft 365 Developer Program to get a free Microsoft 365 subscription.

Register an application

You can register an application using the Azure Active Directory admin center, or by using the Microsoft Graph PowerShell SDK.

Azure Active Directory admin center

  1. Open a browser and navigate to the Azure Active Directory admin center and login using a personal account (aka: Microsoft Account) or Work or School Account.

  2. Select Azure Active Directory in the left-hand navigation, then select App registrations under Manage.

  3. Select New registration. Enter a name for your application, for example, Go Graph Tutorial.

  4. Set Supported account types as desired. The options are:

    OptionWho can sign in?
    Accounts in this organizational directory onlyOnly users in your Microsoft 365 organization
    Accounts in any organizational directoryUsers in any Microsoft 365 organization (work or school accounts)
    Accounts in any organizational directory ... and personal Microsoft accountsUsers in any Microsoft 365 organization (work or school accounts) and personal Microsoft accounts
  5. Leave Redirect URI empty.

  6. Select Register. On the application's Overview page, copy the value of the Application (client) ID and save it, you will need it in the next step. If you chose Accounts in this organizational directory only for Supported account types, also copy the Directory (tenant) ID and save it.

  7. Select Authentication under Manage. Locate the Advanced settings section and change the Allow public client flows toggle to Yes, then choose Save.

PowerShell

To use PowerShell, you'll need the Microsoft Graph PowerShell SDK. If you do not have it, see Install the Microsoft Graph PowerShell SDK for installation instructions.

  1. Open PowerShell and run the RegisterAppForUserAuth.ps1 file with the following command, replacing <audience-value> with the desired value (see table below).

    Note: The RegisterAppForUserAuth.ps1 script requires a work/school account with the Application administrator, Cloud application administrator, or Global administrator role.

    .\RegisterAppForUserAuth.ps1 -AppName "Go Graph Tutorial"-SignInAudience <audience-value>
    SignInAudience valueWho can sign in?
    AzureADMyOrgOnly users in your Microsoft 365 organization
    AzureADMultipleOrgsUsers in any Microsoft 365 organization (work or school accounts)
    AzureADandPersonalMicrosoftAccountUsers in any Microsoft 365 organization (work or school accounts) and personal Microsoft accounts
    PersonalMicrosoftAccountOnly personal Microsoft accounts
  2. Copy the Client ID and Auth tenant values from the script output. You will need these values in the next step.

    SUCCESS Client ID: 2fb1652f-a9a0-4db9-b220-b224b8d9d38b Auth tenant: common

Configure the sample

Open .env and update the values according to the following table.

SettingValue
CLIENT_IDThe client ID of your app registration
TENANT_IDIf you chose the option to only allow users in your organization to sign in, change this value to your tenant ID. Otherwise leave as common.

Run the sample

In your command-line interface (CLI), navigate to the project directory and run the following command.

go run .
close