无需用户登录即可从Azure应用访问令牌
我正在尝试从Azure为我的应用程序获取AccessToken。我一直在尝试第四部分。不幸的是,当我写下面的代码来获取访问令牌时。上面说这个要求不好。这是我正在尝试的代码无需用户登录即可从Azure应用访问令牌,azure,azure-active-directory,Azure,Azure Active Directory,我正在尝试从Azure为我的应用程序获取AccessToken。我一直在尝试第四部分。不幸的是,当我写下面的代码来获取访问令牌时。上面说这个要求不好。这是我正在尝试的代码 string gUrl = "https://login.microsoftonline.com/0000000000-49d7-9734-d930e2db05de/oauth2/v2.0/token"; string ss = "{"+
string gUrl = "https://login.microsoftonline.com/0000000000-49d7-9734-d930e2db05de/oauth2/v2.0/token";
string ss = "{"+
"\"client_id\": \"some Id of Client\","+
"\"scope\": \"https%3A%2F%2Fgraph.microsoft.com%2F.default\"," +
"\"client_secret\": \"\"," +
"\"grant_type\": \"client_credentials\""+
"}";
var payload = new StringContent(JsonConvert.DeserializeObject(ss).ToString(), Encoding.UTF8, "application/x-www-form-urlencoded");
public static string GraphToken(string urlService,StringContent postValue)
{
try
{
string request = string.Empty;
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Clear();
//httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", token))));
using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage(new HttpMethod("POST"), urlService) { Content = postValue })
{
var httpResponseMessage = httpClient.SendAsync(httpRequestMessage).Result;
if (httpResponseMessage.IsSuccessStatusCode)
{
request = httpResponseMessage.Content.ReadAsStringAsync().Result;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("success");
Console.ResetColor();
}
}
}
return request;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
有人能帮我吗?如果你想在没有用户登录的情况下获得访问令牌,你可以使用下面的代码,我认为这比在代码中调用http请求更容易
using Microsoft.Identity.Client;
using System;
using System.Threading.Tasks;
namespace ConsoleApp27
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create("<client id>")
.WithTenantId("<tenant id>")
.WithClientSecret("<client secret>")
.Build();
var scopes = new string[] { "https://graph.microsoft.com/.default" };
var authResult = await confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync();
Console.WriteLine(authResult.AccessToken);
}
}
}
使用Microsoft.Identity.Client;
使用制度;
使用System.Threading.Tasks;
名称空间控制台App27
{
班级计划
{
静态异步任务主(字符串[]args)
{
控制台。WriteLine(“你好,世界!”);
IConfidentialClientApplication-secretentialclientapplication=secretentialclientapplicationbuilder
.创建(“”)
.WithTenantId(“”)
.WithClientSecret(“”)
.Build();
变量范围=新字符串[]{”https://graph.microsoft.com/.default" };
var authResult=await SecretentialClientApplication.AcquireTokenForClient(scopes.ExecuteAsync();
Console.WriteLine(authResult.AccessToken);
}
}
}
你只需要安装这个就可以运行上面的代码。如果你想在没有用户登录的情况下获得访问令牌,你可以使用下面的代码,我认为这比在代码中调用http请求更简单
using Microsoft.Identity.Client;
using System;
using System.Threading.Tasks;
namespace ConsoleApp27
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create("<client id>")
.WithTenantId("<tenant id>")
.WithClientSecret("<client secret>")
.Build();
var scopes = new string[] { "https://graph.microsoft.com/.default" };
var authResult = await confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync();
Console.WriteLine(authResult.AccessToken);
}
}
}
使用Microsoft.Identity.Client;
使用制度;
使用System.Threading.Tasks;
名称空间控制台App27
{
班级计划
{
静态异步任务主(字符串[]args)
{
控制台。WriteLine(“你好,世界!”);
IConfidentialClientApplication-secretentialclientapplication=secretentialclientapplicationbuilder
.创建(“”)
.WithTenantId(“”)
.WithClientSecret(“”)
.Build();
变量范围=新字符串[]{”https://graph.microsoft.com/.default" };
var authResult=await SecretentialClientApplication.AcquireTokenForClient(scopes.ExecuteAsync();
Console.WriteLine(authResult.AccessToken);
}
}
}
您只需安装此程序即可运行上述代码。您需要将这些值作为URL编码的表单数据发送,而不是JSON。您需要将这些值作为URL编码的表单数据发送,而不是JSON。您好@Parth,我可以知道代码是否工作吗?如果工作正常,请将其作为答案(单击我的答案旁边的复选标记,将其从灰色切换为填充)。提前谢谢~当然。我还没试过,请跟我说几个小时。。我会更新。抱歉耽搁了我可以知道代码是否有效吗?如果工作正常,请将其作为答案(单击我的答案旁边的复选标记,将其从灰色切换为填充)。提前谢谢~当然。我还没试过,请跟我说几个小时。。我会更新。抱歉耽搁了