gpt4 book ai didi

c# - 使用 Owin 对 Azure AD 用户进行身份验证后,如何以该用户身份静默访问 CRM Online Web API?

转载 作者:行者123 更新时间:2023-12-03 04:29:48 25 4
gpt4 key购买 nike

我使用 Microsoft.Owin 中间件在用户访问我的页面时根据 Azure AD 对用户进行身份验证。

App_Start > Startup.cs

using Owin;
using System.Configuration;
using System.Globalization;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;
using Microsoft.Owin.Extensions;

[assembly:OwinStartup(typeof(MyApp.App_Start.Startup))]
namespace MyApp.App_Start
{
public class Startup
{
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];

string authority = string.Format(CultureInfo.InvariantCulture, aadInstance, tenant);

public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}

public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions() { });

app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = (context) =>
{
context.HandleResponse();
context.Response.Redirect("/Error/message=" + context.Exception.Message);

return System.Threading.Tasks.Task.FromResult(0);
}
}
});

app.UseStageMarker(PipelineStage.Authenticate);
}
}
}

这工作正常,他们登录后我可以使用诸如 ClaimsPrincipal 之类的东西来获取有关该用户的信息,例如他们的 ID:

string signedInUserID
= ClaimsPrincipal.Current.FindFirst(System.IdentityModel.Claims.ClaimTypes.NameIdentifier).Value;
string tenantID
= ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
string userObjectID
= ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

我现在想要以该用户身份针对 CRM Online 进行静默身份验证,以便可以从 Web API 提取数据。这可能吗?如果可能的话,如何实现?

<小时/>

我一直在尝试按照示例通过 AuthenticationContext 获取访问 token ,我似乎成功了,但随后我无法查询 CRM Online,收到各种有关安全性的错误。为了简洁起见,我不会在这里列出所有代码,除非有人特别需要查看它。 Here's the MSDN article that gives the basics .

我可以使用 CrmConnection 类和连接字符串以及硬编码在 as documented in this MSDN article 中的用户名和密码来访问 CRM Online但这不是我想要的。我想以当前登录的用户身份进行身份验证。

以下是我一直在尝试遵循的一些示例,以防对其他人有用:https://github.com/azure-samples?query=active-directory

这是我找到的最接近我想要的帖子,但它仍然需要用户名/密码... Using ADAL C# as Confidential User /Daemon Server /Server-to-Server - 401 Unauthorized

最佳答案

CRM Online 似乎不支持任何类型的匿名或被动身份验证,并且必须始终提供有效的用户名和密码。

我只是简单地创建一个仅用于 API 访问的用户,并使用这些凭据通过 CRM 进行身份验证。虽然这不是我想要的,但它工作得很好。

我希望有人能证明我错了,但现在似乎不可能。

关于c# - 使用 Owin 对 Azure AD 用户进行身份验证后,如何以该用户身份静默访问 CRM Online Web API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38640306/

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