gpt4 book ai didi

c# - .Net Core 不支持 UserPasswordCredential

转载 作者:行者123 更新时间:2023-12-02 23:44:45 24 4
gpt4 key购买 nike

我必须使用 Microsoft graph API 从 Azure Active Directory 获取组成员。为此我需要授权 token 。我在 .NET 中有一个方法作为用户向 API 进行身份验证,并且我使用 UserPasswordCredential 方法来获取访问 token (通过传递用户名和密码)。

private static string aadInstance =
ConfigurationManager.AppSettings["ida:AADInstance"];
private static string tenant =
ConfigurationManager.AppSettings["ida:Tenant"];
private static string clientId =
ConfigurationManager.AppSettings["ida:ClientId"];
private static string graphResourceId =
ConfigurationManager.AppSettings["ida:GraphResourceId"];
private static string graphApiVersion =
ConfigurationManager.AppSettings["ida:GraphApiVersion"];
private static string graphApiEndpoint =
ConfigurationManager.AppSettings["ida:Gra`phEndpoint"];
private static string appKey =
ConfigurationManager.AppSettings["ida:appKey"];
private static string authority =
String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
Uri redirectUri = new
Uri(ConfigurationManager.AppSettings["ida:RedirectUri"]);
private AuthenticationContext authContext = null;
private ClientCredential clientCredential = null;

public MainWindow()
{
InitializeComponent();
authContext = new AuthenticationContext(authority);
clientCredential = new ClientCredential(clientId, appKey);
CheckForCachedToken();
}

public async void GetToken()
{
AuthenticationResult result = null;
try
{
UserCredential uc = new UserCredential("username", "password");
result = await authContext.AcquireTokenAsync(graphResourceId, clientId, uc);
}
catch (AdalException ex)
{
if (ex.ErrorCode != "user_interaction_required")
{
MessageBox.Show(ex.Message);
}
return;
}
}

但是当我将解决方案升级到 .Net Core 时,这已损坏。即 .NET Core 不支持 UserPasswordCredential 类。有什么解决办法吗?

最佳答案

在 dot net core 中,设计不支持这一点。不推荐对用户名和密码进行硬编码来对 Azure AD 进行身份验证。在大多数情况下,登录将通过 login.live.com 进行,或者如果您使用其他身份提供商(例如 google、facebook 等),这将是他们的登录页面。

看起来您正在使用 Windows 应用程序,您可以检查 options为了它。

Samples各种认证选项可供引用。

基于 token 的身份验证流程是实现此目的的好方法。放弃用户名/密码选项的全部意义在于,如果您的应用程序以某种方式处理用户名或密码,那么它是不安全的。这就是为什么身份提供者有责任这样做。如果您不想这样做,您可以选中仅应用程序选项 Is a browser required for Onedrive/Graph Authentication

您还可以查看 Microsoft Authenticator 的使用情况应用程序是一个选项。但这主要是为了向您的移动应用程序添加第二因素身份验证。

关于c# - .Net Core 不支持 UserPasswordCredential,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53334265/

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