External http endpoint worker role, no acces right with executionContext elevated - wcf

Yet another post on http service on worker role.
Hello everyone,
I have been looking everywhere for an answer and I can't find any.
Here is the problem :
I have a worker role with a tcp entry point. It works fine locally and on azure. So far so good.
I have a windows phone app (sdk 8.0, .net 4.5) that I want to connect to this endpoint.
First problem here, the only way to access a wcf service with a windows phone project is to Right-click project, "Add service reference" and put the service address. BUT ! it doesn't work with tcp.net://{address}:{port}/{service name}.
I've searched and found that I need a http service.
All right then, I'll create a http endpoint. Code is easy, can be found everywhere.
Now here's the interesting part, I have the following error :
HTTP could not register URL http://+:8080/myservice. Your process does not have access rights to this namespace.
So I've looked it up on the internet and found 2 answers :
adding
<Runtime executionContext="elevated" />
to the ServiceDefinition.csdef file.
And setting
var binding = new BasicHttpBinding { HostNameComparisonMode = HostNameComparisonMode.Exact };
Now here's the kicker, I've done that and it still doesn't work.
Here are my Endpoints in ServiceDefinition.csdef file.
<Endpoints>
<InputEndpoint name="External" protocol="tcp" port="10100" />
<InputEndpoint name="Http" protocol="http" port="8080" />
</Endpoints>
Looking for help.

Related

Exception AADSTS50011 is thrown when using azure ad authentication

I have been developing a C# MVC application. Using azure ad to authenticate the users.
But when deployed on IIS, it can only works when accessing the website by "https://localhost". But an error is thrown when accessing the website by "https://machinename".
AADSTS50011: The reply URL specified in the request does not match the reply URLs configured for the application: '4e392b7a-####-####-####-####'.
2 urls are configured on the azure ad authentication:
https://machinename/
https://localhost/
The configuration in the file web.config:
<add key="ClientId" value="4e392b7a-####-####-####-############" />
<add key="Tenant" value="72f988bf-####-####-####-############" />
<add key="Authority" value="https://login.microsoftonline.com/{0}/v2.0" />
<add key="RedirectUri" value="https://machinename/" />
<add key="PostLogoutRedirectUri" value="https://machinename/KeepAlive/SignOut" />
According to your error message, you must ensure that the reply URL in the Azure portal is exactly the same as the reply URL configured by the application.
I have answered similar questions before, There is a general solution to the problem of not match:
When you visit the application url , you will be redirected to the login page. Decode the authorization request URL, you will find redirect_uri, copy the value of redirect_uri and paste it into the azure portal, and try again.
Update:
Thanks for the solution provided by #Tom. For this error, you also need to add the port :
https://machinename/{port}
https://localhost/{port}

Mule 4 HTTP Request Client to access OAuth 2 end point

There is an external service (lets say "https://external-service.com/service") which is secured by OAuth2. I have client ID (Lets say "123_my_client_id"), Secret ID ("324_mysecret") and the access token URL ( lets say "https://access-token.com/access-token") which returns me the token.
I want to access this service with my Mule 4 Http Request. I followed this https://docs.mulesoft.com/connectors/http/http-authentication#oauth2-client-credentials, but couldn't find any workable solution.
This can be done in Mule 3.9. but still struggling to set up this http request configuration for Mule 4. Can anyone please help to setup this request config .
In order to migrate the request authentication to Mule 4,the config now belongs in the http:request-connection component and the HTTP authentication configuration must be placed within an http:authentication component. This applies to all authentication types supported: basic, digest, NTLM and OAuth2.
From the example in the link provided:
<http:request-config name="HTTP_Request_Configuration"
host="some.api.com" port="80" basePath="/api/1.0">
<oauth:client-credentials-grant-type
clientId="your_client_id" clientSecret="your_client_secret"
tokenUrl="http://some.api.com/api/1.0/oauth/token"
scopes="access_user_details, read_user_files">
</oauth:client-credentials-grant-type>
</http:request-config>
Changes to something like:
<http:request-config name="HTTP_Request_Configuration">
<http:request-connection host="some.api.com" port="80">
<http:authentication>
<oauth:client-credentials-grant-type
clientId="your_client_id" clientSecret="your_client_secret"
tokenUrl="http://some.api.com/api/1.0/oauth/token" scopes="access_user_details, read_user_files" />
</http:authentication>
</http:request-connection>
</http:request-config>
Studio may complain about the oauth element, but it should start up fine. Just ignore it.
To anyone wondering how to do that through the user interface, it is pretty easy but the mulesoft documentation could be more clear.
I ended up finding out how to do it, here's the step-by-step process :
create your HTTP Request processor
choose the display name you want in "Display Name:"
choose the desired method in the "Method:" dropdown menu (ex. : GET)
set the path to the endpoint you wish to reach in "Path:"
then create a HTTP_Request_Configuration, by clicking the "+" on the right of "Configuration:" in the "Basic Settings" box
In the configuration window, set up the protocol (HTTP or HTTPS)
In the configuration window, set up the port (80 for HTTP and 443 for HTTPS)
In the configuration window, set up the host (ex. : www.url-to-request.com)
Scroll down to find the "Authentication:" dropdown menu
Choose "Client credentials grant type"
Set up you client-id and client-secret
Set up scopes if required by the application provider
And finally set up the "Token url" (url given by the application provider to retrieve the oauth identification token)
Click OK
After doing this, your oauth2 request should be working. As said in the mulesoft documentation, each time you run the project, a token will be automatically generated. You then can execute as many request as you want without worrying about the authentication anymore.

How to change WCF service address from a vb.net app

I post this questions after lot of time reading different messages about WCF.
I have developed a WFC service that is working at an specific location.
I have developed a vb.net app that consum the WCF service mentioned. It works fine.
I have now a new choice. How can I change from the app the address of the service?. I mean, suppose the service is located at IP a.b.c.d, and now has changed to e.f.d.r. How can I change this on the app?. Should I modify the app.config of the app in execution time? should be possible? Isn't another way to change the address?
A piece of the app.config I'm using on the app is the following:
< bindings>
< basicHttpBinding>
< binding name="BasicHttpBinding_IWCF_ServicioWeb" />
< /basicHttpBinding>
< /bindings>
< client>
< endpoint address="http://localhost:49311/WCF_ServicioWeb.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IWCF_ServicioWeb"
contract="MiServicioWeb.IWCF_ServicioWeb" name="BasicHttpBinding_IWCF_ServicioWeb" />
< /client>
I hope someone could help me...
Just add an ApplicationSettings entry to your config file
<MyApp.Properties.Settings>
<setting name="MyServiceUrl" serializeAs="String">
<value>http://a.b.com/ServicioWeb.svc</value>
</setting>
</MyApp.Properties.Settings>
Now, every time you need to call the service write
Using wcf = New ServicioClient("BasicHttpBinding_IWCF_ServicioWeb",
New EndpointAddress(YourAppNameSpace.Properties.Settings.Default.MyServiceUrl))
.... call your wcf methods ....
End Using
I see that you are using basicHttpBinding. If you are not using either Transport or Message security and certificate validation is not an issue then it's just a matter of changing the Endpoint Address both on the service (server) and in the client application that is consuming the WCF service.

Running WebApi alongside MVC project in Azure

I have a solution with an MVC project and also a separate Web Api project. I have been working on the the MVC portion and have had it hosted as a web role in a cloud service project.
Now I need to build on the Web Api. Running the solution as is in the azure emulator I'm unable to hit my Web Api controller, I get a 404 Not Found.
What steps do I need to take after creating the WebApi project in the solution in order to enable my MVC project alongside it to hit the Api controller using a jQuery ajax call (from the MVC project)?
Note: The portion of the application that will be consuming the Api is in a subdomain (ex "consumingapp.myapplication.com/api/Values". I don't know if that makes any difference.
Ok here are the steps to run MVC and WebApi side by side in Azure under a single web role:
Add only the MVC project as a Web Role in the Cloud Service project.
Open ServiceDefinition.csdef an configure as follows:
<Sites>
<Site name="Web" physicalDirectory="..\..\..\Application.Web">
<Bindings>
<Binding name="Endpoint1" endpointName="Endpoint1" />
</Bindings>
</Site>
<Site name="Web.Api" physicalDirectory="..\..\..\Application.Web.Api">
<Bindings>
<Binding name="Endpoint1" endpointName="Endpoint1" hostHeader="api.myapplication.com"/>
</Bindings>
</Site>
</Sites>
Note:
The physical directory is relative to the definition file that is deployed to CloudProject\bin\release so you need to back track 3 nodes to get to the solution folder. Also I had some trouble with this trying to run the project it would automatically switch the physicalDirectory back to a bad path. To fix that I right clicked the cloud project > Project Dependencies > make sure both projects are checked off. Also make sure to save and close all instances of the text editor for the definition file then Rebuild and finally Run. Seems like some kind of bug that was driving me nuts and eventually disappeared.
Configure your host files to allow for local testing using the emulator:
127.0.0.1 myapplication.com api.myapplication.com
To use this .NET cors implementation run the following Nuget command on your WebApi project:
Install-Package Thinktecture.IdentityModel
Add the following class (taken from the post) to your WebApi project:
public static class CorsConfig
{
public static void RegisterCors(HttpConfiguration httpConfiguration)
{
WebApiCorsConfiguration corsConfig = new WebApiCorsConfiguration();
corsConfig.RegisterGlobal(httpConfiguration);
corsConfig
.ForResources("Values") //Controller nae
.ForOrigins("http://myapplication.com", "myapplication.com:81")
.AllowAll();
}
}
Call RegisterCors() from Application_Start()
Make sure you Web.config in the WebApi project has OPTIONS verb for the ExtensionlessUrlHandler:
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
Make cross-origin request to hit your api controller:
$.ajax({ url: "http://api.myapplication.com/api/Values", ... })
And Viola!
Hope that helps others

How to detect user agent in WCF web service

How can I detect the user agent in a web service? My web service is implemented using a WCF webservice with basicHTTPBinding. It will be a post from some SOAP clients. I wish to know the user-agent from the clients.
I shall like to see some sample code for this.
I am using a WCF based web service and in the svc.cs, I tried to catch this.Context.Request.UserAgent. But it gives the following error:
this.Context.Request.UserAgent 'MySoapService.MyService' does not contain a definition for 'Context' and no extension method 'Context' accepting a first argument of type 'MySoapService.MyService' could be found (are you missing a using directive or an assembly reference?)
I also tried System.Web.HttpContext.Current.Request.UserAgent and it says:
'System.Web.HttpContext.Current' is null
Edit note:
I tried to activate the ASP.NET compatibility mode. I added <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> in the config file and added [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] on the top of the class that implements the service interface. Then using System.Web.HttpContext.Current.Request.UserAgent gives me the user agent as desired.
There is another way to get the user agent without enabling ASP.NET compatibility in web.config:
string userAgent = WebOperationContext.Current.IncomingRequest.Headers["User-Agent"];
You can use also:
WebOperationContext.Current.IncomingRequest.UserAgent
You can read user agent from the HttpContext.Current.Request object if you enable ASP.NET compatibility in web.config:
What a totally unhelpful response!
This is not a trivial task. Yes it is obviously possible to get te user-agent string but how does one actually do it? I spent 2 hours checking google and so on but found the answer buried in MSDN documentation. In Visual Studio, from within a WebMethod try
this.Context.Request.UserAgent
That should do it!
User-Agent is a standard HTTP header. It'll be available to your web service just like it's available to anything CGI-like.
Did you even bother searching for this before posting your question? There must be millions of hits for it on Google.