我正在尝试调用CRM Dynamics On Premise 2016 Web API。 我使用OAuth配置了授权代码流,并且可以正常工作。但是,我需要设置“客户端凭据”流,因为许多应用程序在后台运行,并且无法通过登录屏幕进行提示。 由于其前提,我们没有Azure AD。
答案 0 :(得分:0)
可以通过客户端凭据访问Xrm api,而无需进行任何特殊设置(无论是本地还是在线)-您只需设置具有适当权限的S2S用户,就可以像这样登录他:
static void InContext(Action<IOrganizationService> callback, Org org)
{
var credentials = new ClientCredentials();
if (!org.IsLocal)
{
credentials.UserName.UserName = org.UserName;
credentials.UserName.Password = org.Password;
}
else
{
credentials.Windows.ClientCredential = new NetworkCredential(org.UserName, org.Password);
}
using (var serviceProxy =
new OrganizationServiceProxy(new Uri(org.OrganizationServiceUri),
null, credentials
, null))
{
callback.Invoke(serviceProxy);
}
}
public class Org
{
public string UserName { get; set; }
public string Password { get; set; }
public string OrganizationServiceUri { get; set; }
public bool IsLocal { get; set; }
public Org()
{
}
public Org(bool isLocal, string userName, string password, string organizationServiceUri)
{
IsLocal = isLocal;
UserName = userName;
Password = password;
OrganizationServiceUri = organizationServiceUri;
DiscoveryServiceUri = discoveryServiceUri;
}
}
然后在您的后端代码中:
var org = new Org(true, "Administrator", "Password",
"http://ondracrm/org/XRMServices/2011/Organization.svc");
InContext((os) => {
// some sample work with organization service
var response = (RetrieveEntityResponse)os.Execute(
new RetrieveEntityRequest
{
LogicalName = "contact",
EntityFilters = EntityFilters.Attributes
});
}, org);