[英]After authenticating a user against Azure AD using Owin how can I silently access CRM Online Web API as this user?
我正在使用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
這樣的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
獲取訪問令牌,我似乎成功了,但后來我無法查詢CRM Online,我收到各種關於安全性的錯誤。 為簡潔起見,我不會在此列出所有代碼,除非有人特別需要查看它。 這是MSDN文章,提供了基礎知識 。
我可以使用CrmConnection
類訪問CRM Online,並使用硬編碼的用戶名和密碼連接字符串, 如本MSDN文章中所述,但這不是我想要的。 我想作為當前登錄用戶進行身份驗證。
以下是我一直試圖遵循的一些示例,以防它們對其他人有用: https : //github.com/azure-samples?query = active-directory
這是我發現的最接近我想要的帖子,但它仍然需要用戶名/密碼... 使用ADAL C#作為機密用戶/守護程序服務器/服務器到服務器 - 401未經授權
CRM Online似乎不支持任何類型的匿名或被動身份驗證,並且必須始終提供有效的用戶名和密碼。
我只是簡單地創建了一個僅用於API訪問的用戶,並使用這些憑據對CRM進行身份驗證。 雖然這不是我想要的,但確實可行。
我喜歡有人證明我錯了,但現在似乎不可能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.