Authenticate a user in MS teams custom app using asp.net and c#

%3CLINGO-SUB%20id%3D%22lingo-sub-1526052%22%20slang%3D%22en-US%22%3EAuthenticate%20a%20user%20in%20MS%20teams%20custom%20app%20using%20asp.net%20and%20c%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1526052%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20am%20working%20on%20a%20custom%20app%20for%20MS%20teams.%20It%20is%20not%20a%20bot%20but%20it%20is%20a%20tab%20app.%26nbsp%3B%20I%20need%20to%20authenticate%20logged%20in%20user%20to%20MS%20teams%20using%20c%23.%20Please%20let%20me%20know%20how%20to%20authenticate%20user%20and%20show%20the%20user%20profile%20of%20logged%20in%20user.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1526052%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1526113%22%20slang%3D%22en-US%22%3ERe%3A%20Authenticate%20a%20user%20in%20MS%20teams%20custom%20app%20using%20asp.net%20and%20c%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1526113%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F141855%22%20target%3D%22_blank%22%3E%40Rajesh%20Lohar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUse%20REST%20API%20service%20provided%20by%20Microsoft%20inside%20your%20application.%3C%2FP%3E%3CP%3Eyou%20can%20authenticate%20the%20user%20and%20and%20fetch%20user%20details%20using%20API.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20services%20%2C%20be%20it%20Tabs%2C%20Bots%20etc.%2C%20everything%20uses%20the%20same%20authentication%20process.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20use%20the%20following%20links%20to%20get%20more%20idea%20of%20how%20to%20do%20it.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.c-sharpcorner.com%2Farticle%2Fauthentication-with-office-365-in-C-Sharp%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.c-sharpcorner.com%2Farticle%2Fauthentication-with-office-365-in-C-Sharp%2F%3C%2FA%3E%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoftgraph%2Faspnet-snippets-sample%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fmicrosoftgraph%2Faspnet-snippets-sample%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Echeers.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1526139%22%20slang%3D%22en-US%22%3ERe%3A%20Authenticate%20a%20user%20in%20MS%20teams%20custom%20app%20using%20asp.net%20and%20c%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1526139%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F726607%22%20target%3D%22_blank%22%3E%40gk_vasudev%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3EThank%20you%20so%20much%20for%20your%20reply.%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20it%20possible%20that%20user%20is%20logged%20in%20to%20MS%20teams%20already%20so%20it%20can%20auto%20login%20to%20custom%20app%20pages%20using%20same%20credentials%3F%3C%2FP%3E%3CP%3EAs%20per%20your%20answer%2C%20user%20has%20to%20login%20by%20his%20Office%20365%20credentials%20again%20in%20the%20app.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1526189%22%20slang%3D%22en-US%22%3ERe%3A%20Authenticate%20a%20user%20in%20MS%20teams%20custom%20app%20using%20asp.net%20and%20c%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1526189%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F141855%22%20target%3D%22_blank%22%3E%40Rajesh%20Lohar%3C%2FA%3E%26nbsp%3BYou%20can%20implement%20SSO%20for%20tab.%20SSO%20picks%20up%20the%20credentials%20of%20the%20user%20from%20teams%20login%20and%20the%20user%20does%20not%20have%20to%20enter%20the%20credentials%20again.%20Here%20is%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fmicrosoftteams%2Fplatform%2Ftabs%2Fhow-to%2Fauthentication%2Fauth-aad-sso%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Edocs%3C%2FA%3E%20for%20SSO%20and%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fmicrosoftteams%2Fplatform%2Ftabs%2Fhow-to%2Fauthentication%2Fauth-aad-sso%23sample-code%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Esample%3C%2FA%3E.%20But%2C%20currently%20we%20do%20not%20have%20a%20complete%20sample%20in%20C%23.%20But%20since%20this%20is%20just%20a%20web%20page%20you%20can%20still%20use%20it%20in%20your%20C%23%20solution.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1526212%22%20slang%3D%22en-US%22%3ERe%3A%20Authenticate%20a%20user%20in%20MS%20teams%20custom%20app%20using%20asp.net%20and%20c%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1526212%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F359599%22%20target%3D%22_blank%22%3E%40Gousia_Begum%3C%2FA%3E%26nbsp%3B.%3C%2FP%3E%3CP%3EPlease%20let%20me%20know%20if%20you%20have%20complete%20sample%20in%20c%23.%20The%20information%20shared%20by%20you%20is%20really%20useful.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1526230%22%20slang%3D%22en-US%22%3ERe%3A%20Authenticate%20a%20user%20in%20MS%20teams%20custom%20app%20using%20asp.net%20and%20c%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1526230%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F141855%22%20target%3D%22_blank%22%3E%40Rajesh%20Lohar%3C%2FA%3E%26nbsp%3BUnfortunately%2C%20we%20do%20not%20have%20a%20complete%20sample%20for%20SSO%20in%20C%23.%20But%20you%20can%20use%20the%20code%20from%20the%20NodeJS%20sample%20in%20your%20app's%20web%20page.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1526229%22%20slang%3D%22en-US%22%3ERe%3A%20Authenticate%20a%20user%20in%20MS%20teams%20custom%20app%20using%20asp.net%20and%20c%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1526229%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F141855%22%20target%3D%22_blank%22%3E%40Rajesh%20Lohar%3C%2FA%3E%3CBR%20%2F%3Eif%20thats%20the%20case%20%2C%3CBR%20%2F%3EThen%20u%20could%20a%20request%20an%20authentication%20token%20from%20microsoft%20teams%3CBR%20%2F%3Eand%20use%20the%20token%20information%20to%20authenticate%20the%20user%20to%20your%20application.%3CBR%20%2F%3Eby%20this%20way%20the%20user%20would%20be%20automatically%20logs%20in%20to%20your%20custom%20application.%3CBR%20%2F%3E%3CBR%20%2F%3EThis%20is%20%22Single%20Sign-on%22%20Concept%20based%20on%20the%20%22on-behalf-of%22%20flow.%3CBR%20%2F%3E%3CBR%20%2F%3EPlease%20go%20through%20these%20links%20to%20get%20a%20clear%20idea%20about%20the%20process%20and%20how%20to%20do%20it.%3CBR%20%2F%3Eits%20mostly%20in%20Js%2C%20but%20i%20think%20you%20would%20be%20able%20to%20figure%20out%20to%20do%20it%20in%20asp.net%20and%20c%23%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fmicrosoftteams%2Fplatform%2Ftabs%2Fhow-to%2Fauthentication%2Fauth-aad-sso%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fmicrosoftteams%2Fplatform%2Ftabs%2Fhow-to%2Fauthentication%2Fauth-aad-sso%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fdevelop%2Fv2-oauth2-on-behalf-of-flow%23service-to-service-access-token-request%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fdevelop%2Fv2-oauth2-on-behalf-of-flow%23service-to-service-access-token-request%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fdevelop%2Factive-directory-v2-protocols%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fdevelop%2Factive-directory-v2-protocols%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi,

I am working on a custom app for MS teams. It is not a bot but it is a tab app.  I need to authenticate logged in user to MS teams using c#. Please let me know how to authenticate user and show the user profile of logged in user.

 

Thanks,

 

6 Replies

Hi @Rajesh Lohar 

 

 

Use REST API service provided by Microsoft inside your application.

you can authenticate the user and and fetch user details using API.

 

Any services , be it Tabs, Bots etc., everything uses the same authentication process.

 

You can use the following links to get more idea of how to do it.

https://www.c-sharpcorner.com/article/authentication-with-office-365-in-C-Sharp/

https://github.com/microsoftgraph/aspnet-snippets-sample

 

cheers.

Hi @gk_vasudev ,

Thank you so much for your reply. 

Is it possible that user is logged in to MS teams already so it can auto login to custom app pages using same credentials?

As per your answer, user has to login by his Office 365 credentials again in the app.

 

Thanks,

 

 

@Rajesh Lohar You can implement SSO for tab. SSO picks up the credentials of the user from teams login and the user does not have to enter the credentials again. Here is the docs for SSO and the sample. But, currently we do not have a complete sample in C#. But since this is just a web page you can still use it in your C# solution.

Thank you @Gousia_Begum .

Please let me know if you have complete sample in c#. The information shared by you is really useful.

 

Thanks

@Rajesh Lohar
if thats the case ,
Then u could a request an authentication token from microsoft teams
and use the token information to authenticate the user to your application.
by this way the user would be automatically logs in to your custom application.

This is "Single Sign-on" Concept based on the "on-behalf-of" flow.

Please go through these links to get a clear idea about the process and how to do it.
its mostly in Js, but i think you would be able to figure out to do it in asp.net and c#

https://docs.microsoft.com/en-us/microsoftteams/platform/tabs/how-to/authentication/auth-aad-sso

https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-on-behalf-of-flow#service-...

https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols

@Rajesh Lohar Unfortunately, we do not have a complete sample for SSO in C#. But you can use the code from the NodeJS sample in your app's web page.