How require authorization within whole ASP .NET MVC application

Simplest way is to add Authorize attribute in the filter config to apply it to every controller. public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); //Add this line filters.Add(new AuthorizeAttribute()); } } Another way is to have all of your controllers inheriting from a base class. This is something I do … Read more

Get ExtraData from MVC5 framework OAuth/OWin identity provider with external auth provider

Recently I had to get access to Google profile’s picture as well and here is how I solve it… If you just enable the code app.UseGoogleAuthentication(); in the Startup.Auth.cs file it’s not enough because in this case Google doesn’t return any information about profile’s picture at all (or I didn’t figure out how to get … Read more

Correct way to use HttpContext.Current.User with async await

As long as your web.config settings are correct, async/await works perfectly well with HttpContext.Current. I recommend setting httpRuntime targetFramework to 4.5 to remove all “quirks mode” behavior. Once that is done, plain async/await will work perfectly well. You’ll only run into problems if you’re doing work on another thread or if your await code is … Read more

How to return a PDF from a Web API application

Some Server side code to return PDF (Web Api). [HttpGet] [Route(“documents/{docid}”)] public HttpResponseMessage Display(string docid) { HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest); var documents = reader.GetDocument(docid); if (documents != null && documents.Length == 1) { var document = documents[0]; docid = document.docid; byte[] buffer = new byte[0]; //generate pdf document MemoryStream memoryStream = new MemoryStream(); MyPDFGenerator.New().PrintToStream(document, memoryStream); … Read more

Get UserID of logged-in user in Asp.Net MVC 5

The answer is right there in your code. What does this return? var userID = User.Identity.GetUserId(); If you are using ASP.NET Identity, then after logging in (and redirecting to another page), the IPrincipal.IIdentity should be a ClaimsIdentity. You can try this: var claimsIdentity = User.Identity as ClaimsIdentity; if (claimsIdentity != null) { // the principal … Read more

How can I implement a theme from bootswatch or wrapbootstrap in an MVC 5 project?

The steps to apply a theme are fairly simple. To really understand how everything works together, you’ll need to understand what the ASP.NET MVC 5 template is providing out of the box and how you can customize it for your needs. Note: If you have a basic understanding of how the MVC 5 template works, … Read more

Async PartialView causes “HttpServerUtility.Execute blocked…” exception

Child actions must be invoked synchronously. Issue 601 I am also not aware of any recent updates to the current MVC libraries allowing this functionality. A comment on Issue 601, hints at this functionality being added in MVC vNext, aka. MVC6. Child actions look to be replaced with ViewComponent which can be invoked asynchronously from … Read more

ASP.NET MVC5/IIS Express unable to debug – Code Not Running

For me the solution was a much simpler one. In my Solution Explorer in Visual Studio, I right click on the web project, chose properties and then navigated to the “web” tab. From there I changed the Project URL to another port number. For example, if it was http://localhost:1052 – I changed it to http://localhost:4356. … Read more