需要验证CRM凭证 [英] Need to validate CRM credentials

查看:106
本文介绍了需要验证CRM凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标很简单。使用Dynamics CRM 2013 API并提供了URL,用户名,密码和域,如何检查值是否有效?

My goal is simple. Using the Dynamics CRM 2013 API and given a url, user name, password, and domain, how can I check that the values are valid?

我有一个自定义应用程序,该应用程序可以将使用API​​。我有一个设置屏幕,用户将在其中输入URL,用户名,密码和域。我还有一个名为测试连接的按钮,它将验证它是否可以使用该信息进行连接。

I have a custom application that will be using the API. I have a setup screen where the user will enter the URL, user name, password, and domain. I also have a button called "Test Connection" that will verify that it can connect using that information.

这是我用来创建连接的逻辑:

Here is the logic I am using to create the connection:

string connectionString;
if (_crmDomain != "")
    connectionString = string.Format(@"Url={0}; Username={1}\{2}; Password={3}", url, domain, userName, password);
else
    connectionString = string.Format(@"Url={0}; Username={1}; Password={2}", url, userName, password);

var css = new ConnectionStringSettings("CRMConnectionString", connectionString);
var cn = new CrmConnection(css);
var service = new OrganizationService(cn);

问题是,即使凭据无效(也许密码不正确),创建的行新的OrganizationService可以正常工作。

The issue is that even if the credentials are invalid (perhaps an incorrect password), the line that creates the new OrganizationService works fine. Is there any way to check that it will work when an actual call is eventually made?

现在我可以通过进行虚拟呼叫来解决它。这迫使它创建连接。这是我在做什么:

Right now I am getting around it by making a dummy call. This forces it to create the connection. Here is what I’m doing:

var request = new RetrieveAllEntitiesRequest();
request.EntityFilters = EntityFilters.Entity;
service.Execute(request);

如果凭据无效,则service.Execute行将引发异常。这可以很好地解决一个问题。假设用户输入了有效的凭据并成功单击了测试连接。现在,假设他们将密码更改为错误,然后单击测试连接。在这种情况下,它仍然成功。似乎使用了以前存在的连接。有没有办法清除或清除以前的连接,以便当我尝试使用无效值时会抛出异常?

If the credentials are not valid then the service.Execute line throws an exception. This works fine with one problem. Let’s say the user entered valid credentials and successfully clicked Test Connection. Now let’s say they change the password to be wrong and click Test Connection. In this case it is still succeeding. It seems to use the previously existing connection. Is there a way to wipe out or clear the previous connection so that when I try with invalid values it will throw an exception?

再次,我的总体目标是验证登录值。我追求的是能提供最佳解决方案的一切。谢谢!

Again, my overall goal is to validate the login values. Whatever will provide the best way of accomplishing this is what I’m after. Thanks!

推荐答案

放弃使用 ConnectionStringSettings 并使用 CrmConnection.Parse()

var cn = CrmConnection.Parse(connectionString);
var service = new OrganizationService(cn);

try
{           
    service.Execute(new WhoAmIRequest());
}
catch (Exception ex)
{
    //Do something because connection setup was bad
}

更新

我运行了以下代码并获得了预期的输出,显示在代码下方。将密码更改为错误密码后,请求将失败,而将密码更改为正确密码后,请求将成功。

I ran the following code and got the expected output, shown below the code. When the password is changed to a bad password the request fails and when changed to a good password it succeeds.

var connectionString = @"Url=https://server.com/orgname/;Username=domain\username;Password=goodpassword;";
var cn = CrmConnection.Parse(connectionString);
var service = new OrganizationService(cn);

try
{           
    service.Execute(new WhoAmIRequest());
    Console.WriteLine("Good Password");
}
catch
{
    Console.WriteLine("Bad Password");    
}

connectionString = @"Url=https://server.com/orgname/;Username=domain\username;Password=badpassword;";
cn = CrmConnection.Parse(connectionString);
service = new OrganizationService(cn);

try
{           
    service.Execute(new WhoAmIRequest());
    Console.WriteLine("Good Password");
}
catch
{
    Console.WriteLine("Bad Password");    
}

connectionString = @"Url=https://server.com/orgname/;Username=domain\username;Password=goodpassword;";
cn = CrmConnection.Parse(connectionString);
service = new OrganizationService(cn);

try
{           
    service.Execute(new WhoAmIRequest());
    Console.WriteLine("Good Password");
}
catch
{
    Console.WriteLine("Bad Password");    
}

控制台输出为:

Good Password
Bad Password
Good Password

这篇关于需要验证CRM凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆