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);
dataStream.Close();
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.