Automate SPFX single sign on using C#

Suresh Rajamani 1 Reputation point
2021-10-11T05:59:34.497+00:00

Team,
We have developed SPFX application and it is enabled with Azure AD SSO for login.
I want to generate the same SPFX access token using C# by passing userid and password to automate it for testing. I need this access token and generate bearer token using ADAL to call my web api. So that i can test my web api from C#. Kindly guide me to generate SSO access using user, password and then generate bearer token using ADAL. I dont want to prompt for getting user, password.

Thanks,
Suresh Rajamani

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,605 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,896 questions
SharePoint Workflow
SharePoint Workflow
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Workflow: An orchestrated and repeatable pattern of business activity, enabling data transformation, service provision, and information retrieval.
569 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Siva-kumar-selvaraj 15,631 Reputation points
    2021-10-13T10:33:00.797+00:00

    @Suresh Rajamani

    Hello Suresh,

    Thanks for reaching out.

    You can leverage Microsoft identity platform and OAuth 2.0 Resource Owner Password Credentials flow to authenticate and get access_token for respective resource by using User credential.

    To know more about ROPC (Resource Owner Password Credentials ) : https://video2.skills-academy.com/en-us/azure/active-directory/develop/v2-oauth-ropc
    Scenarios and supported authentication flows: https://video2.skills-academy.com/en-us/azure/active-directory/develop/authentication-flows-app-scenarios#scenarios-and-supported-authentication-flows

    I would recommend you to use Microsoft Authentication Libraries (MSAL) instead ADAL library which is in deprecation cycle. Please take a look at the sample apps that use MSAL.

    Hope this helps.

    Adding SharePoint Dev tags into this thread to get suggestion and expertise from SharePoint perspective.

    ------
    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.


  2. RaytheonXie_MSFT 34,661 Reputation points Microsoft Vendor
    2021-10-14T09:19:36.19+00:00

    Hi @Suresh Rajamani ,
    We can retrieve user AccessToken by using the OfficeDevPnP.Core library

    static void Main(string[] args)  
    {  
        string siteUrl = "https://tenant.sharepoint.com";  
        string userName = "user@tenant.onmicrosoft.com";  
        string Password = "********";  
    
        var accessToken = GetUserAuthToken(siteUrl,userName, Password);  
        Console.Read();  
    }  
    
    private static string GetUserAuthToken(string siteUrl, string userPrincipal, string Pwd)  
    {  
        var authManager = new AuthenticationManager();  
        var context = authManager.GetAzureADCredentialsContext(siteUrl, userPrincipal, Pwd);  
        Console.WriteLine(context.GetAccessToken());  
        return context.GetAccessToken();  
    }  
    

    As sikumars-msft said the ADAL already Deprecated. We will recommend you to use MSAL
    Please refer to following link
    https://video2.skills-academy.com/en-us/azure/active-directory/develop/msal-net-acquire-token-silently


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.