I need access to CRM Dynamics 365 i use below code to access its worked fine with "ADFS (IFD) Authentication", but with CRM Online "Dynamics 365 https://OrgName.api.crm4.dynamics.com/XRMServices/2011/Organization.svc"
getting this Error:
An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.
Server stack trace:
at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.ProcessReply(Message reply, SecurityProtocolCorrelationState correlationState, TimeSpan timeout)
at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at System.ServiceModel.Security.IWSTrustContract.Issue(Message message)
at System.ServiceModel.Security.WSTrustChannel.Issue(RequestSecurityToken rst, RequestSecurityTokenResponse& rstr)
at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1.Issue(AuthenticationCredentials authenticationCredentials)
at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1.AuthenticateInternal(AuthenticationCredentials authenticationCredentials)
at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1.AuthenticateWithOrgIdForACS(AuthenticationCredentials authenticationCredentials, Uri identifier)
at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1.AuthenticateOnlineFederationInternal(AuthenticationCredentials authenticationCredentials)
at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1.Authenticate(AuthenticationCredentials authenticationCredentials)
at Microsoft.Xrm.Sdk.Client.OrganizationServiceConfiguration.Authenticate(AuthenticationCredentials authenticationCredentials)
This my code:
public static IOrganizationService GetService()
{
try
{
OrganizationServiceProxy _serviceProxy;
string userName = "";
string password = "";
string domain = "";
Uri serviceUri;
serviceUri = new Uri(ConfigurationManager.AppSettings["URL"]);
userName = ConfigurationManager.AppSettings["UserName"];
password = ConfigurationManager.AppSettings["Password"];
domain = ConfigurationManager.AppSettings["domain"];
IServiceManagement<IOrganizationService> orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(serviceUri);
if (orgServiceManagement.AuthenticationType == AuthenticationProviderType.Federation)
{
// ADFS (IFD) Authentication
ClientCredentials clientCred = new ClientCredentials();
clientCred.UserName.UserName = userName;
clientCred.UserName.Password = password;
_serviceProxy = new OrganizationServiceProxy(orgServiceManagement, clientCred);
}
else if (orgServiceManagement.AuthenticationType == AuthenticationProviderType.OnlineFederation)
{
// Online authentication
AuthenticationCredentials authCredentials = new AuthenticationCredentials();
authCredentials.ClientCredentials.UserName.UserName = userName;
authCredentials.ClientCredentials.UserName.Password = password;
// Error getting here
AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);
_serviceProxy = new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);
}
else
{
// On-Premise, non-IFD authentication
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new NetworkCredential(userName, password, domain);
_serviceProxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
}
_serviceProxy.EnableProxyTypes();
}
catch (Exception e)
{
}
return (IOrganizationService)_serviceProxy;
}
*This post is locked for comments