gpt4 book ai didi

c# - 调用 AddUserAsync .NET Core 2.0 时出现 PlatformNotSupported 异常

转载 作者:行者123 更新时间:2023-11-30 15:55:36 35 4
gpt4 key购买 nike

我正在尝试编写一些代码,使用图形 API 在 Azure AD 中创建用户。我从网上的一个例子开始,但现在添加用户时失败了,在线

等待 adClient.Users.AddUserAsync(userGraphObj);

在下面的 CreateUser() 方法中。我得到的错误是

PlatformNotSupportedException: Secure binary serialization is not supported on this platform.

我正在使用 .NET Core 2.0,在 Windows 7 上调试。谷歌搜索,我发现他们为 2.0 恢复了序列化,but only for specific types .

我真的不在乎。如何在代码中将用户添加到 Azure AD?


const String appClientID = "2be733f1-88c3-6482-8e2a-5e9631fc3a32";
const String tenant = "espn.onmicrosoft.com";
const String authString = "https://login.microsoftonline.com/" + tenant;
const String authClientSecret = "dDdaVGee315s65ewDSWEwfdw7wq5efDNO5C3cvN4RA";
const String resAzureGraphAPI = "https://graph.windows.net";
const String serviceRootURL = resAzureGraphAPI + appClientID;

private ActiveDirectoryClient GetAADClient()
{
Uri serviceRoot = new Uri(serviceRootURL);
ActiveDirectoryClient adClient = new ActiveDirectoryClient(
serviceRoot, async () => await GetAppTokenAsync());
return adClient;
}

private static async Task<String> GetAppTokenAsync()
{
AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
ClientCredential clientCred = new ClientCredential(appClientID, authClientSecret);
AuthenticationResult authResult = await authenticationContext.AcquireTokenAsync(resAzureGraphAPI, clientCred);
return authResult.AccessToken;
}

public async Task<IActionResult> CreateUser()
{
var adClient = GetAADClient();

//Construct The User
String userEmail = "TestUser@example.com";
String mailNickname = userEmail.Split(new char[] { '@' }).FirstOrDefault();

var userGraphObj = new Microsoft.Azure.ActiveDirectory.GraphClient.User()
{
GivenName = "Test",
Surname = "User",
Mobile = "13133124044",
MailNickname = mailNickname,
DisplayName = "Test User",
AccountEnabled = true
};

await adClient.Users.AddUserAsync(userGraphObj);

return Ok(tempPassword);

}

最佳答案

Microsoft 本身建议不再使用 Azure AD Graph API,而推荐使用 Microsoft Graph API(参见 blog post)。

如果您对使用 Azure AD API 的要求不是很高,请按照以下步骤通过最新的 API 创建用户。

免责声明:

  • 我从未设法从桌面应用程序成功获取 token
  • 我还没有真正理解应该如何使用权限范围(这里似乎需要一个 URL,但在示例中它通常是一个字符串列表,例如 User.ReadWrite.AllDirectory.ReadWrite.All)

获取 token 的代码:

const String appClientID = "2be733f1-88c3-6482-8e2a-5e9631fc3a32";
const String tenant = "brazzers.onmicrosoft.com";
const String authString = "https://login.microsoftonline.com/" + tenant;
const String authClientSecret = "dDdaVGee315s65ewDSWEwfdw7wq5efDNO5C3cvN4RA";

public static GraphServiceClient GetAuthenticatedClient()
{
var delegateAuthenticationProvider = new DelegateAuthenticationProvider(
async (requestMessage) =>
{
var accessToken = await GetAppTokenAsync();
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
}
);

return new GraphServiceClient(delegateAuthenticationProvider);
}

private static async Task<String> GetAppTokenAsync()
{
// this doesn't work for desktop apps,
// and PublicClientApplication throws a NotImplementedException
var cca = new ConfidentialClientApplication(
appClientID,
authString,
"http://www.example.com/", // no redirect
new ClientCredential(authClientSecret),
new TokenCache(),
new TokenCache());

var authResult = await cca.AcquireTokenForClientAsync(new[] { $"https://graph.microsoft.com/.default" });
return authResult.AccessToken;
}

创建用户的代码(由 samples 提供):

public async Task<User> CreateUser(GraphServiceClient graphClient)
{
// This snippet gets the tenant domain from the Organization object to construct the user's email address.
var organization = await graphClient.Organization.Request().GetAsync();
var domain = organization.CurrentPage[0].VerifiedDomains.ElementAt(0).Name;

// Add the user.
var userEmail = "TestUser@" + domain;
var mailNickname = userEmail.Split(new char[] { '@' }).FirstOrDefault();

return await graphClient.Users.Request().AddAsync(new User
{
AccountEnabled = true,
DisplayName = "Test User",
MailNickname = mailNickname,
PasswordProfile = new PasswordProfile
{
Password = "super_strong_password"
},
UserPrincipalName = userEmail
});
}

关于c# - 调用 AddUserAsync .NET Core 2.0 时出现 PlatformNotSupported 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48385996/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com