Access SOAP fr .NET MVC Controller w/ Static IP
Table of contents
Note: This example assumes your QUOTAGUARD_URL environment variable is set and contains your unique connection string.
Here we show how to use QuotaGuard to access a SOAP service from a .NET MVC web controller. This example should also apply to other WebRequests made from an MVC controller.
A common use case is from your MVC controller you want to access an IP-whitelisted service that uses SOAP but the web service object does not have a “proxy” property that allows you to specify the QuotaGuard proxy service.
Solution: Create a proxy object and use it as a property of the WebRequest MVC class, which will send all following requests via the proxy.
The solution is split in to two parts, the configuration in your web.config (which on Azure can be done from your management console) and the MVC Controller itself.
MVC Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bank.Webservices; // fictitious namespace name for your webservice's reference
using System.Diagnostics;
using System.Xml;
using System.Net;
using System.Web.Configuration;
public class PaymentController : Controller
{
[HttpPost]
// this is usually a form result, so attribute of action is HttpPost
public ActionResult Index(string anyTokenYouUseWithYourBank)
{
string QuotaGuardProxyAddress = WebConfigurationManager.AppSettings["QuotaGuard_Proxy_URI"];
// to get the Proxy Uri from Web.config
string QuotaGuardUser = WebConfigurationManager.AppSettings["QuotaGuard_User"];
// to get the Proxy User from Web.config
string QuotaGuardPassword = WebConfigurationManager.AppSettings["QuotaGuard_Password"];
// to get the Proxy Password from Web.config
var proxy = new WebProxy(QuotaGuardProxyAddress);
// we create a new proxy object
proxy.Credentials = new NetworkCredential(QuotaGuardUser, QuotaGuardPassword);
// we give the proxy the QuotaGuard credentials
WebRequest.DefaultWebProxy = proxy;
// thanks to WebRequest, from here on every request of this controller will pass through the proxy
var soapClientObject = new WSBankSoapClient(); //fictitious name for the banks's web service
var result = soapClientObject .GrabSomethingOnTheNet(anyTokenYouUseWithYourBank);
// now our WS call goes passes Proxy and in the variable result we have the web service's result
// we use the result here
return Redirect("http://newpage");
}
}
Configuration (web.config)
<!-- keys for QuotaGuard-->
<add key="QuotaGuard_Proxy_URI" value="http://yourQuotaGuardUser:yourQuotaGuardPassword@eu-west-1-babbage.getstatica.io:9293"/>
<add key="QuotaGuard_User" value="yourQuotaGuardUser"/>
<add key="QuotaGuard_Password" value="yourQuotaGuardPassword"/>
<!-- end of keys for QuotaGuard-->
...
</appSettings>
This solution was kindly contributed by Riccardo Moschetti, a Microsoft MCSD Web Applications specialist.