PDA

View Full Version : Authenticating to web services in C#



Ilion Blaze
11-24-2006, 11:19 PM
I've having some issues authenticating to the FlexNet web services in a C# client I'm developing. Here's the code I'm using:



macroLic.getFulfillmentsQueryRequestType cdpRequest = new global::formpost.macroLic.getFulfillmentsQueryRequestType();
macroLic.getFulfillmentCountRequestType count = new global::formpost.macroLic.getFulfillmentCountRequestType();

macroLic.fulfillmentsQueryParametersType cparams = new global::formpost.macroLic.fulfillmentsQueryParametersType();
string key = "sampleLicense";
macroLic.SimpleQueryType keyQuery = new global::formpost.macroLic.SimpleQueryType();
keyQuery.value = key;
cparams.activationId = keyQuery;
cparams.activationId.searchType = macroLic.simpleSearchType.EQUALS;
cdpRequest.queryParams = cparams;
//count.queryParams = cparams;
count.queryParams = cparams;

macroLic.LicenseService lic = new global::formpost.macroLic.LicenseService();
System.Net.NetworkCredential netCred = new System.Net.NetworkCredential("user", "password");
System.Net.CredentialCache myCred = new System.Net.CredentialCache();
myCred.Add("http://host:port/flexnet/services/LicenseService", 8888, "Basic", netCred);
lic.Credentials = myCred;
lic.PreAuthenticate = true;

lic.Url = "http://host:port/flexnet/services/LicenseService";

macroLic.getFulfillmentCountResponseType response = lic.getFulfillmentCount(count);

if (response.statusInfo.status.HasValue)
{
string r = response.responseData.ToString();
MessageBox.Show(r);
}


Of course sampleLicense, host, and port all have proper values in the actual code. Anyone have any suggestions?

lemairel
03-15-2007, 09:15 AM
Hello,

I am encountering the exact same problem. Did you find a solution ?

Regards,

L. LEMAIRE

imhttran
04-12-2007, 10:51 AM
Depending on what version of the Publisher you are using. We recently switch from 5.0 to 6.1. We now have issue with authentication. They have blocked this option out if you are not a premier user. Not cool .

RobertDickau
04-12-2007, 12:12 PM
One way to handle the FLEXnet Operations web-service authentication in a C# client (this example generated with the "Add Web Reference" functionality in Visual Studio 2005) is to add the authentication to the request header; in the generated proxy class (for the ProductPackagingService, in this example), look for this:

public partial class ProductPackagingService : ...
And add code similar to this:

protected override WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest request;
request = (HttpWebRequest)base.GetWebRequest(uri);

if (PreAuthenticate)
{
NetworkCredential networkCredentials =
Credentials.GetCredential(uri, "Basic");
if (networkCredentials != null)
{
byte[] credentialBuffer =
new
System.Text.UTF8Encoding( ).GetBytes(
networkCredentials.UserName + ":" +
networkCredentials.Password);

request.Headers["Authorization"] =
"Basic " +
Convert.ToBase64String(credentialBuffer);
}
else
{
throw new ApplicationException(
"No network credentials");
}
}
return request;
}This overridden function specifies that requests are to use "Basic" HTTP authentication, adding a new "Authorization" header with credentials of the form "Basic " plus the base-64-encoded "Name:Password" string. (You might need to add using System.Net; to the generated class.)

In the client code, then, you can use code similar to the following:

ProductPackagingService pps = new ProductPackagingService( );

NetworkCredential netCredential =
new NetworkCredential("Name", "Password");
Uri uri = new Uri(pps.Url);

ICredentials credentials =
netCredential.GetCredential(uri, "Basic");

pps.Credentials = credentials;
pps.PreAuthenticate = true;

// etc.


(Regarding comments about versions 5.0 and 6.1: if you're referring to FLEXnet Connect (formerly Update Service), you might consider posting to this forum (http://community.macrovision.com/forumdisplay.php?f=72).)