Go to definition disabled in Visual Studio 2013

Posted by on Jul 9, 2014 in Asp.net

I recently ran in to the odd problem of the ‘Go To Definition’ content menu entry being disabled. F12 also didn’t work which made code navigation a major problem. I have no idea why, but the following solution fixed it for me: Close Visual Stidio In the solution directory, ensure hidden files are enabled Delete the hidden .sou file that contains all the user options Restart visual studio and re-build your solution The downside to this is that it deletes all your user specific settings for the solution (e.g. in an asp.net application you will lose custom start pages, IIS settings and so on). If anyone knows a way to fix this without deleting the file, please contact...

Read More »

Unable to create new MVC project in Visual Studio 2013

Posted by on Jun 27, 2014 in Asp.net, MVC

I recently had a problem in Visual Studio 2013 whereby I couldn’t create a new MVC project. When I tried, an empty solution would be created and a dialog box would be shown with the following: System cannot find the file specified (0x80070002) After a bit of digging about, it appears this occurs when there is an issue with Nuget – either a faulty installation or its somehow been corrupted. Thankfully fixing it was as simple as you’d imagine. In visual Studio 2013 go to: Tools=> Extensions and Updates => Installed => Nuget Package Manager for Visual Studio 2013 => Uninstall Close VS when you’re asked to restart it and then download a fresh copy from here by ruinning the VSIX file: Nuget from the Visual Studio Gallery Once the installation is complete, it should be working again… Hope that...

Read More »

Using OAuth in .net without Membership

Posted by on Oct 30, 2013 in Asp.net, MVC

If you search for anything to do with OAuth (Open Authorization), you’ll find lots of very comprehensive examples of how to get started using OAuth in .net 4.5 and VS2013 from the in built templates. You’ll also find a few .net 4.0 examples which also show you how to get started with OAuth, but pretty much everything described is tightly couples with asp.net membership. If, like me, you’re working on a project that uses its own authentication and authorization implementation, or you simply don’t want to take a dependency on a database of any kind, then the information is harder to get hold of. What is OAuth? At its simplest, OAuth allows you to log in to one site (site A) using your credentials from another site (site B) without ever providing your details to site A. This is beneficial for a number of reasons: Fewer credentials to remember for the user The hosting site doesn’t need to concern itself with handling this kind of sensitive data Rich profile information can be obtained (if permitted), enhancing the user experience Allows the user control over which sites can authenticate them and they can revoke this control at any time Many of the largest websites on the internet either offer OAuth login functionality (including the likes of StackOverflow, Vimeo, Digg, Disqus, Tripadvisor etc), or act as a provider (e.g. Facebook, Microsoft, Google, Yahoo, LinkedIn etc). It’s pretty safe to say, if you’ve logged in to any other site using your Facebook account, you’ve used OAuth. Lets get started In the following example I’ll be using VS2013, but VS2012 should also work just fine.   Create a new solution in Visual Studio:   File->New->VisualC#->Web->ASP.NET Web Application   Be sure to select Empty Web Application for the template and notice that Authentication is set to no authentication   Once the project has been created, open up NuGet (right-click on References and select ‘manage NuGet packages’) and search for the Microsoft WebPages OAuth Library (see screenshot).   This will install the library and a host of dependencies – fortunately none of these are membership dependencies. Once complete, if you look in your references folder you should find a number of OAuth references Register with Facebook   I’d recommend following the steps under the section ‘Registering with an external provider’ shown here: http://www.asp.net/mvc/tutorials/security/using-oauth-providers-with-mvc to set up and configure your Facebook application. Once complete you should have an App ID and a Secret key which we’ll need shortly. Set up OAuth Before your application starts it will be necessary to register the provider(s) you wish to offer. To accomplish this, create a new static class in the APP_START folder called OAuthProviders.cs and within a single static method called Configure. This is your first interaction with the OAuth libraries and where you will supply your credentials for Facebook: C# using Microsoft.Web.WebPages.OAuth; namespace OAuthSample.App_Start { public static class OAuthProviders...

Read More »

Enabling HTML files to be processed by the asp.net pipeline

Posted by on Oct 10, 2013 in Asp.net

A quick how-to here. I recently needed to process all HTML files through the ASP.Net pipeline so I could take advantage of its security features. The obvious solution to this would be to run all modules through the pipeline by adding the following to your web.config: C# <modules runAllManagedModulesForAllRequests="true" /> 1 <modules runAllManagedModulesForAllRequests="true" /> However, there would be a performance hit then on other static file types such as CSS, JavaScript and images. The correct way to achieve this is through the following web.config changes: C# <system.web> <compilation debug="true" targetFramework="4.0"> <buildProviders> <add extension =".html" type="System.Web.Compilation.PageBuildProvider"/> <add extension =".htm" type="System.Web.Compilation.PageBuildProvider"/> </buildProviders> </compilation> </system.web> 12345678 <system.web>  <compilation debug="true" targetFramework="4.0">    <buildProviders>      <add extension =".html" type="System.Web.Compilation.PageBuildProvider"/>      <add extension =".htm" type="System.Web.Compilation.PageBuildProvider"/>    </buildProviders>  </compilation></system.web> This tells the runtime that the .htm and .html extensions should be picked up and processed by the PageBuildProvider via System.Web.dll Then, to ensure this is correctly processed by IIS or IISExpress (or any other asp.net capable web server), add the following section, also to your web.config file: C# <system.webServer> <handlers> <add name="HTML" path="*.html" verb="GET, HEAD, POST, DEBUG" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script"/> <add name="HTM" path="*.htm" verb="GET, HEAD, POST, DEBUG" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script"/> </handlers> </system.webServer> 12345678 <system.webServer>        <handlers>            <add name="HTML" path="*.html" verb="GET, HEAD, POST, DEBUG"                 type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script"/>            <add name="HTM" path="*.htm" verb="GET, HEAD, POST, DEBUG"                 type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script"/>        </handlers>    </system.webServer> The above handlers instruct the runtime to run all requests for GET, POST, HEAD or DEBUG verbs through the PageHandlerFactory. With this in place you can then handle HTML files through the pipeline, which is ideal for scenarios such as integrating the asp.net security architecture into static sites containing HTML files rather than a WebForms or MVC...

Read More »

Getting SignalR to talk to a hub on another domain

Posted by on Sep 30, 2013 in JavaScript, SignalR

I recently met the challenge of getting SignalR to talk to a hub on a different domain to the one it was running on the client. Finding working and up-to-date information on how to do this is a challenge given the rapid release cycle of the SignalR library. This raises all kinds of security concerns as the same-origin request issue comes-to-light. However, it can be done using the following configuration. In the project file, edit the Global.asax file as follows: C# protected void Application_Start(object sender, EventArgs e) { RouteTable.Routes.MapHubs(new HubConfiguration() { EnableCrossDomain = true }); } 1234 protected void Application_Start(object sender, EventArgs e){  RouteTable.Routes.MapHubs(new HubConfiguration() { EnableCrossDomain = true });} This instructs the SignalR ‘server’ component (i.e. the hub) to accept requests from domains other than that the site is currently configured on. Without this the request will be blocked. That’s the server side taken care of, but CORS is a major client-side security concern (although not much these days given the prevalence of client-side JavaScript services). On the client, the proxy used to communicate with the server needs to be re-directed to the new host. If you don’t do this, signalR will try and initiate a hub connection on the current domain, so will make a request to /signalr/negotiate/?_=123456789 Which will be on the same host/domain as the site is running, not the alternate domain intended to be targeted. Therefore you will need to set the URL for the client connection as follows: var connection = $.connection(window.location.protocol + "//url.to.the.server/"); $.connection.hub.proxies["hubName"].connection.url = window.location.protocol + "//url.to.the.server/signalr" 12 var connection = $.connection(window.location.protocol + "//url.to.the.server/");$.connection.hub.proxies["hubName"].connection.url = window.location.protocol + "//url.to.the.server/signalr" note: the window.location.protocol just ensures the connection retains the HTTP/HTTPS setting then, when starting the hub, be sure to tell it that it will be connecting cross-domain: C# $.connection.hub.start({ xdomain: true }); 1 $.connection.hub.start({ xdomain: true }); Once you’ve done the above, you should be good to...

Read More »

Getting started with OWIN

Posted by on Sep 29, 2013 in Asp.net

OWIN If you’re a developer in the web space, especially focusing or involved with the Microsoft stack, you’ve no doubt heard a lot about OWIN lately. OWIN, or Open Web Interface for .NET is an open-source initiative somewhat inspired by the Rack project on the Ruby platform and the increasingly popular Node.js movement. Like Rack, OWIN sets out to provide a flexible, lightweight middleware between web servers and the underlying framework components. OWIN also delivers on its promise of portability and modularity meaning applications written on top of OWIN are server and potentially even OS agnostic. If, like me, you’ve created a simple web server using Node.js in a dozen lines of code, then self-hosted via a command line, you’ve no doubt been slightly envious as the elegance and autonomy of that solution compared to the tightly coupled MS/IIS/asp.net stack. However, you also soon probably found that a lot of the goodness asp.net gives you ‘out of the box’ goes a long way, and with OWIN, developers can potentially have the best of both worlds. Katana If you’re a Microsoft web developer then for every OWIN you’ve probably heard two Katana’s. Katana is Microsofts (also open-source) implementation of the OWIN specification. It can be found on Codeplex and is unsurprisingly geared to provision for the Microsoft hosts and frameworks. An Example Lets put this into an example. Using Visual Studio, click File, New Project and select Console App Once that’s completed, we need to grab a few packages from NuGet. From the package manager console (or via the GUI if you’d prefer), obtain the following packages: install-package Microsoft.Owin.Hosting –IncludePreRelease install-package Microsoft.Owin.Host.HttpListener –IncludePreRelease Once the process completes you’ll notice a few dependencies have been imported for you via the magic of NuGet dependency management: Now your app is good to go. It’s quite basic: C# using Microsoft.Owin.Hosting; using System; namespace Owin.ConsoleApp { class Program { static void Main(string[] args) { string uri = "http://localhost:8001"; using (WebApp.Start<StartUp>(uri)) { Console.WriteLine("App started"); Console.ReadKey(); Console.WriteLine("App stopping..."); } } } } 1234567891011121314151617181920 using Microsoft.Owin.Hosting;using System; namespace Owin.ConsoleApp{    class Program    {        static void Main(string[] args)        {            string uri = "http://localhost:8001";            using (WebApp.Start<StartUp>(uri))            {                Console.WriteLine("App started");                Console.ReadKey();                Console.WriteLine("App stopping...");            }        }    }} The static WebApp class has a generic Start method which takes in a URL on which to listen and a TStartup parameter which is a class that defines our configuration for the application. As per the above, create a new class inside the project called Startup.cs as follows: C# namespace Owin.ConsoleApp { class StartUp { public void Configuration(IAppBuilder app) { app.Run(ctx => { ctx.Response.ContentType = "text/plain"; return ctx.Response.WriteAsync("Helo from OWIN"); }); } } } 12345678910111213141516 namespace Owin.ConsoleApp{    class StartUp    {         public void Configuration(IAppBuilder app)        {            app.Run(ctx =>            {                ctx.Response.ContentType = "text/plain";                return ctx.Response.WriteAsync("Helo from OWIN");            });        }     }} Everything should compile just fine – hit F5 to start the app. A console window should appear telling you the app has started. Next, just open your browser and go to the Uri specified in the app (in this...

Read More »