page_type | description | products | languages | extensions | urlFragment | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sample |
Microsoft Teams app SSO for Tab, Bot, ME - search, action, linkunfurl |
|
|
|
officedev-microsoft-teams-samples-app-sso-csharp |
This app talks about the Teams Tab, Bot, ME - search, action, linkunfurl SSO with C#
Tab SSO This sample shows how to implement Azure AD single sign-on support for tabs. It will
- Obtain an access token for the logged-in user using SSO
- Call a web service - also part of this project - to exchange this access token
- Call Graph and retrieve the user's profile
Bot, ME SSO Bot Framework v4 bot using Teams authentication
This bot has been created using Bot Framework, it shows how to get started with authentication in a bot for Microsoft Teams.
The focus of this sample is how to use the Bot Framework support for oauth in your bot. Teams behaves slightly differently than other channels in this regard. Specifically an Invoke Activity is sent to the bot rather than the Event Activity used by other channels. This Invoke Activity must be forwarded to the dialog if the OAuthPrompt is being used. This is done by subclassing the ActivityHandler and this sample includes a reusable TeamsActivityHandler. This class is a candidate for future inclusion in the Bot Framework SDK.
The sample uses the bot authentication capabilities in Azure Bot Service, providing features to make it easier to develop a bot that authenticates users to various identity providers such as Azure AD (Azure Active Directory), GitHub, Uber, etc. The OAuth token is then used to make basic Microsoft Graph queries. Refer the SSO setup documentation.
IMPORTANT: The manifest file in this app adds "token.botframework.com" to the list of
validDomains
. This must be included in any bot that uses the Bot Framework OAuth flow.
-
A global administrator account for an Office 365 tenant. Testing in a production tenant is not recommended! You can get a free tenant for development use by signing up for the Office 365 Developer Program (not a guest account).
-
To test locally, you'll need Ngrok installed on your development machine. Make sure you've downloaded and installed Ngrok on your local machine. ngrok will tunnel requests from the Internet to your local computer and terminate the SSL connection from Teams.
NOTE: The free ngrok plan will generate a new URL every time you run it, which requires you to update your Azure AD registration, the Teams app manifest, and the project configuration. A paid account with a permanent ngrok URL is recommended.
- Required Permissions
* Make sure you have the following Graph permissions enabled:
email
,offline_access
,openid
,profile
, andUser.Read
(default). * For permissionsManage > API Permissions
* Our SSO flow will give you access to the first 4 permissions, and we will have to exchange the token server-side to get an elevated token for theprofile
permission (for example, if we want access to the user's profile photo).
- Setup for Bot SSO Refer to Bot SSO Setup document.
NOTE: Create Bot Framework registration resource in Azure - Use the current
https
URL you were given by running ngrok. Append with the path/api/messages
used by this sample - Ensure that you've enabled the Teams Channel - If you don't have an Azure account you can use this Bot Framework registration
-
Clone the repository
git clone https://github.com/OfficeDev/microsoft-teams-samples.git
-
Run the bot from a terminal or from Visual Studio:
- Launch Visual Studio
- File -> Open -> Project/Solution
- Navigate to
samples/csharp_dotnetcore/app_sso
folder - Select
app_sso.sln
file - Press
F5
to run the project
-
Run ngrok - point to port
3978
ngrok http -host-header=localhost 4000
-
Update the
appsettings.json
configuration for the bot to use theMicrosoftAppId
(Microsoft App Id),MicrosoftAppPassword
(App Password) andconnectionName
(OAuth Connection Name) from the Bot Framework registration.
NOTE: the App Password is referred to as the
client secret
in the azure portal and you can always create a new client secret anytime.
- This step is specific to Teams.
- Edit the
manifest.json
contained in theteamsAppManifest
folder to replace your Microsoft App Id (that was created when you registered your bot earlier) everywhere you see the place holder string<<YOUR-MICROSOFT-APP-ID>>
(depending on the scenario the Microsoft App Id may occur multiple times in themanifest.json
) also update the<<DOMAIN-NAME>>
with the ngrok URL - Zip up the contents of the
teamsAppManifest
folder to create amanifest.zip
- Upload the
manifest.zip
to Teams (in the Apps view click "Upload a custom app")
- Edit the
Note: This
manifest.json
specified that the bot will be installed in a "personal" scope only. Please refer to Teams documentation for more details.
You can interact with this bot by sending it a message. The bot will respond by requesting you to login to AAD, then making a call to the Graph API on your behalf and returning the results.
- Install App
- Type anything on the compose box and send
- The bot will perform
Single Sign-On
and Profile card will be displayed along with the option prompt to view thetoken
NOTE: If the user is using the application for the first time and user consent is required for additional permissions, the following dialog box appears to continue with the consent experience
If the bot couldn't perform
SSO
then it will fallback to normal Authentication method and show aSign In
card like below
- Open
Messaging Extension
(Search), it will show profile details
- Open
Messaging Extension
(Action), it will show profile details
or
- Open
Messaging Extension
(linkunfurl), The link will unfurl and show profile details
Paste https://profile.botframework.com on the compose box
NOTE: If
SSO
couldn't be performed then it will fallback to normal Authentication method and you will get a defaultSign In
action
Consent the ME Search by clicking the Sign In
link like below
Consent the ME Action by clicking the Setup
button like below
- Open
SSO Tab
, Continue and then Accept and it'll show the profile details
To learn more about deploying a bot to Azure, see Deploy your bot to Azure for a complete list of deployment instructions.
- Bot Framework Documentation
- Bot Basics
- SSO for Bot
- SSO for Messaging Extensions
- SSO for Tab
- Azure Portal
- Add Authentication to Your Bot Via Azure Bot Service
- Activity processing
- Azure Bot Service Introduction
- Azure Bot Service Documentation
- Azure CLI
- Azure Portal
- Language Understanding using LUIS
- Channels and Bot Connector Service
- Microsoft Teams Developer Platform