Getting User based bearer token RRS feed

  • Question

  • How can i create a user based bearer token using username of the user.

    I created it following way. But for that it needs user password. i need to do it using only user name.


    private string GetUserAccessToken()
                string clientId = "XXXX";
                string appKey = "XXXX";
                string tenantId = "XXXX";

                string accessToken = string.Empty;
                string apiEndpoint = "https://login.microsoftonline.com/" + tenantId + "/oauth2/v2.0/token";
                WebRequest accessTokenRequest = WebRequest.Create(apiEndpoint);
                accessTokenRequest.Method = "POST";
                accessTokenRequest.ContentType = "application/x-www-form-urlencoded";

                string requestParams = "grant_type=password&client_id=" + clientId + "&client_secret=" + appKey + "&scope=https://graph.microsoft.com/.default" + "&userName=XXXX&password=XXXX";
                byte[] byteArray = Encoding.UTF8.GetBytes(requestParams);
                accessTokenRequest.ContentLength = byteArray.Length;
                Stream dataStream = accessTokenRequest.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                using (WebResponse response = accessTokenRequest.GetResponse())
                    string json = string.Empty;
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        json = reader.ReadToEnd();
                    O365AccessTokenModel accessTokenModel = JsonConvert.DeserializeObject<O365AccessTokenModel>(json);
                    accessToken = accessTokenModel.access_token;
                return accessToken;

    But i need to do it without password. is there ant possible way to do it.

    Tuesday, August 4, 2020 3:49 AM

All replies

  • Hi Shakila Jayarathne,

    Thank you for posting here.

    Many ways to get OAuth 2.0 authentication token are provided in the following link.

    How do I get an OAuth 2.0 authentication token in C#

    >>i need to do it without password

    Hope following reference could be helpful.

    .NET Identity: Generate token without explicit username / password

    Code in the reference build a ticket and then create an access token, then return to the caller.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 5, 2020 6:12 AM