Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Calling application's web services via HttpVPN
Ultidev Team
#1 Posted : Tuesday, May 24, 2011 9:04:46 AM(UTC)
Ultidev Team

Groups: Administration
Joined: 11/3/2005(UTC)
Posts: 2,253

Thanks: 28 times
Was thanked: 60 time(s) in 59 post(s)
UltiDev HttpVPN allows making web service calls to invoke web services hosted by your web applications. Since HttpVPN requires client authentication, HttpVPN user credentials must be passed along with your web service calls. Until HttpVPN rolls out client certificate authentication support, the only option for now is to user HttpVPN username/password combination to obtain an http cookie with an ID token and attach that cookie to all your subsequent requests.

Following walk-through shows how to call HttpVPN authentication web service to obtain an authentication cookie and attach the cookie to the subsequent requests. We use C# and ASP.NET ASMX web service for this article. We used an existing web service (from Cassini Web Server installed locally). The material below shows how an existing web service client can be modified to call a web service over HttpVPN. No modifications to the server side of the web service is required – only client gets modified.

HttpVPN authentication web service WSDL is accessible here.

Phase 1: Start with a regular web service client. We started with creating a simple Console application that consumes Cassini configuration web service. Here's the code:

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace WebServiceClientViaHttpVPN
{
class Program
{
private static void LetsDoIt()
{
// Some sample regular Web Service

// 1. Instantiate client class
Cassini.CassiniConfigurationService businessService = new Cassini.CassiniConfigurationService();

// 2. Call the method a few times
for (int i = 0; i < 3; i++)
{
int portNumber = businessService.GetApplicationPort(new Guid("4fd8b3f7-bc73-4583-95fe-e7b69b10a3ae"));
Console.WriteLine("Cassini Explorer port number is {0}", portNumber);
}
}

static void Main(string[] args)
{
try
{
Program.LetsDoIt();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
}

"Cassini.CassiniConfigurationService" is a sample service, whose GetApplicationPort() method is called a few times in the loop. It's a pretty straightforward stuff.

Phase 2. Say, we registered this web service app as a "public" web application on HttpVPN Portal at www.MyOwnSecureWeb.com. Since any public application made accessible by HttpVPN is a simple pass-through deal, all we need to do to access it via HttpVPN is to change the URL of the web service to the Portal-based one. As you have figured this out already, it will be something like this:

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace WebServiceClientViaHttpVPN
{
class Program
{
private static void LetsDoIt()
{
// Some sample regular Web Service

// 1. Instantiate client class
Cassini.CassiniConfigurationService businessService = new WebServiceClientViaHttpVPN.Cassini.CassiniConfigurationService();

// 1.a Point Web Service client to the HttpVPN Portal
businessService.Url = @"https://myownsecureweb.com/fc4acd63-a608-479b-8ecd-85f8791c4daf/UdVpnQ2Fzc2luaUNvbmZpZ3VyYXRpb25TZXJ2aWNlLmFzbXg=/SecureTunnel.axd"; // You get this URL from the browser’s address bar once you have reached your web service app over HttpVPN.


// 2. Call the method a few times
for (int i = 0; i < 3; i++)
{
int portNumber = businessService.GetApplicationPort(new Guid("4fd8b3f7-bc73-4583-95fe-e7b69b10a3ae"));
Console.WriteLine("Cassini Explorer port number is {0}", portNumber);
}
}

static void Main(string[] args)
{
try
{
Program.LetsDoIt();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
}


However, making a web service public is not what you want. You need to call your web services in a secure, authenticated manner. So to make it protected, you change the registration of your application with HttpVPN Portal to allow only a person with some specific credentials to access the service. Next iteration of this sample code will use Portal's authentication web service to do what browser does when we use forms/cookies authentication: it makes us log on and then gives us auth cookie for the duration of the session. Here's the link to the Portal authentication Web Service WSDL. You will need to use the WSDL link to add a Web Reference to your client application before proceeding. Please use ASMX instead of WCF, or use basicHttpBinding when using WCF.

Phase 3. Add Portal authentication web reference. After that we modify the code to add logging in functionality in to Portal before calling the service:
Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace WebServiceClientViaHttpVPN
{
class Program
{
private static void LetsDoIt()
{
// Instantiate HttpVPN Authentication web service client class
HttpVPN.AuthenticationService httpVpnAuthSvc = new WebServiceClientViaHttpVPN.HttpVPN.AuthenticationService();
// Create a cookie container to keep cookies during the session just like web browser does
httpVpnAuthSvc.CookieContainer = new System.Net.CookieContainer();

// Log in to HttpVPN with your user credentials
if (!httpVpnAuthSvc.Login("<your HttpVPN username>", "<This is a fake password>"))
// Failed to log it
Console.WriteLine("Failed to login to HttpVPN Portal Authentication Service");
else
{ // Logged in successfully
try
{
// Instantiate our business logic web service
Cassini.CassiniConfigurationService businessService = new WebServiceClientViaHttpVPN.Cassini.CassiniConfigurationService();
// Make it send back the cookie created during authentication step
businessService.CookieContainer = httpVpnAuthSvc.CookieContainer;
// User service's HttpVPN Portal-based URL
businessService.Url = @"https://myownsecureweb.com/fc4acd63-a608-479b-8ecd-85f8791c4daf/UdVpnQ2Fzc2luaUNvbmZpZ3VyYXRpb25TZXJ2aWNlLmFzbXg=/SecureTunnel.axd";
// Execute our business logic
for (int i = 0; i < 3; i++)
{
int portNumber = businessService.GetApplicationPort(new Guid("4fd8b3f7-bc73-4583-95fe-e7b69b10a3ae"));
Console.WriteLine("Cassini Explorer port number is {0}", portNumber);
}
}
finally
{
// Regardless whether business logic was successful, let's be efficient
// and release out HttpVPN Portal authentication session by logging out
httpVpnAuthSvc.Logout();
}
}
}

static void Main(string[] args)
{
try
{
Program.LetsDoIt();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
}

All we've done at this point is we called HttpVPN Portal to log into it and store the auth cookie Portal issued for future use. Then we called our business logic web service via Portal, and once we finished with business logic, we log out of HttpVPN Portal. Now this does all you need – calls your secured web service over HttpVPN, but it still has one problem left: if you keep authentication cookie for a while, between your service calls, the cookie may expire. Let's modify the code to handle this case by checking whether there was a problem when calling your web service, and if there was, we will log in again and then re-try calling your web service:

Phase 4:
Code:
using System;
using System.Collections.Generic;
using System.Text;

using System;
using System.Collections.Generic;
using System.Text;

namespace WebServiceClientViaHttpVPN
{
class Program
{
private static void LoginToPortal(HttpVPN.AuthenticationService httpVpnAuthSvc)
{
if (!httpVpnAuthSvc.Login("<your HttpVPN username>", "<This is a fake password>"))
// Failed to log it
throw new Exception("Failed to login to HttpVPN Portal Authentication Service");
}

// main business logic call wrapped in the supporting HttpVPN log in logic
private static int CallWsViaHttpVPN(Guid appID, ref HttpVPN.AuthenticationService httpVpnAuthSvc)
{
if (httpVpnAuthSvc == null)
{
httpVpnAuthSvc = new WebServiceClientViaHttpVPN.HttpVPN.AuthenticationService();
// Create a cookie container to keep cookies during the session just like web browser does
httpVpnAuthSvc.CookieContainer = new System.Net.CookieContainer();

LoginToPortal(httpVpnAuthSvc);
}

// Instantiate our business logic web service
Cassini.CassiniConfigurationService businessService = new WebServiceClientViaHttpVPN.Cassini.CassiniConfigurationService();
// Make it send back the cookie created during authentication step
businessService.CookieContainer = httpVpnAuthSvc.CookieContainer;
// User service's HttpVPN Portal-based URL
businessService.Url = @"https://myownsecureweb.com/fc4acd63-a608-479b-8ecd-85f8791c4daf/UdVpnQ2Fzc2luaUNvbmZpZ3VyYXRpb25TZXJ2aWNlLmFzbXg=/SecureTunnel.axd";

try
{
return businessService.GetApplicationPort(appID);
}
catch
{
// Exception could be due to authentication cookie may have expired.
// Let's try to re-login just in case and try calling the service again:
LoginToPortal(httpVpnAuthSvc);
return businessService.GetApplicationPort(appID);
}
}

private static void LetsDoIt()
{
HttpVPN.AuthenticationService httpVpnAuthSvc = null;

try
{
Guid appID = new Guid("4fd8b3f7-bc73-4583-95fe-e7b69b10a3ae");
for (int i = 0; i < 3; i++)
{
int portNumber = CallWsViaHttpVPN(appID, ref httpVpnAuthSvc);
Console.WriteLine("Cassini Explorer port number is {0}", portNumber);
}
}
finally
{
// Regardless whether business logic was successful, let's be efficient
// and release out HttpVPN Portal authentication session by logging out
httpVpnAuthSvc.Logout();
}
}

static void Main(string[] args)
{
try
{
Program.LetsDoIt();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
}

In the sample above a call to your web service has been placed in a special wrapper, which does additional things, like in case of the exception being thrown when calling your web service, it catches it, re-logs in, and the tries again calling the web service.

We realize that dealing with forms authentication is cumbersome in web services, but this approach is a stop-gap measure. HttpVPN in the future will help application vendors provision client X.509 client certificates during application installation.


Best regards,
UltiDev Team.
Please donate at http://www.ultidev.com/products/Donate.aspx to help us improve our products.
Guest
#2 Posted : Monday, August 25, 2014 12:53:12 PM(UTC)
Groups:

Message was deleted by a Moderator.
Guest
#3 Posted : Saturday, October 11, 2014 2:49:58 PM(UTC)
Groups:

Message was deleted by a Moderator.
Guest
#4 Posted : Tuesday, November 4, 2014 1:30:52 AM(UTC)
Groups:

Message was deleted by a Moderator.
Guest
#5 Posted : Tuesday, November 4, 2014 9:33:25 PM(UTC)
Groups:

Message was deleted by a Moderator.
Guest
#6 Posted : Saturday, November 22, 2014 7:17:20 PM(UTC)
Groups:

Message was deleted by a Moderator.
Guest
#7 Posted : Wednesday, December 31, 2014 8:28:08 PM(UTC)
Groups:

Message was deleted by a Moderator.
Guest
#8 Posted : Thursday, January 1, 2015 7:42:36 AM(UTC)
Groups:

Message was deleted by a Moderator.
Guest
#9 Posted : Monday, February 9, 2015 2:05:01 AM(UTC)
Groups:

Message was deleted by a Moderator.
Rss Feed  Atom Feed
Users browsing this topic
Guest (5)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You can vote in polls in this forum.