Functions Can be used to Authenticate Directly with MSCRM Web services
1. On Premise MSCRM Authenication Function
public static IOrganizationService GetXrmService(string _crmUserName, string _crmURL, string _crmDomain, string _crmPassword)
{
Uri organizationUri = new Uri(_crmURL);
ClientCredentials cCredentials = new ClientCredentials();
cCredentials.UserName.UserName = string.Format("{0}\\{1}", _crmDomain, _crmUserName);
cCredentials.UserName.Password = _crmPassword;
OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, null, cCredentials, null);
orgProxy.EnableProxyTypes();
_xrmService = (IOrganizationService)orgProxy;
return _xrmService;
}
2. Online Authenication function
public static IOrganizationService GetOnlineXrmService(string _crmUserName, string _crmURL, string _crmDomain, string _crmPassword)
{
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = _crmUserName;
credentials.UserName.Password = _crmPassword;
credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
// credentials.Windows.ClientCredential = new System.Net.NetworkCredential(_userId, _password, _domain);
Uri organizationUri = new Uri(_crmURL);
Uri homeRealmUri = null;
using (_serviceProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null))
{
_serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
_xrmService = (IOrganizationService)_serviceProxy;
_orgContext = new OrganizationServiceContext(_xrmService);
}
return _xrmService;
}
No comments:
Post a Comment