Where is Request.IsAjaxRequest() in Asp.Net Core MVC?

I got a little confused, because the title mentioned MVC 5. Search for Ajax in the MVC6 github repo doesn’t give any relevant results, but you can add the extension yourself. Decompilation from MVC5 project gives pretty straightforward piece of code: /// <summary> /// Determines whether the specified HTTP request is an AJAX request. /// … Read more

Dependency injection, inject with parameters

You can either provide a delegate to manually instantiate your cache provider or directly provide an instance: services.AddSingleton<ICacheProvider>(provider => new RedisCacheProvider(“myPrettyLocalhost:6379”)); services.AddSingleton<ICacheProvider>(new RedisCacheProvider(“myPrettyLocalhost:6379”)); Please note that the container will not explicitly dispose of manually instantiated types, even if they implement IDisposable. See the ASP.NET Core doc about Disposal of Services for more info.

How to get the Development/Staging/production Hosting Environment in ConfigureServices

You can easily access it in ConfigureServices, just persist it to a property during Startup method which is called first and gets it passed in, then you can access the property from ConfigureServices. public Startup(IWebHostEnvironment env, IApplicationEnvironment appEnv) { …your code here… CurrentEnvironment = env; } private IWebHostEnvironment CurrentEnvironment{ get; set; } public void ConfigureServices(IServiceCollection … Read more

Use Office Interop on ASP.net MVC6 website

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment. If you are building a solution that runs in a server-side … Read more

How can I call a SQL Stored Procedure using EntityFramework 7 and Asp.Net 5

I hope that I correctly understand your problem. You have existing STORED PROCEDURE, for example dbo.spGetSomeData, in the database, which returns the list of some items with some fields and you need to provide the data from Web API method. The implementation could be about the following. You can define an empty DbContext like: public … Read more

Cookie Authentication expiring too soon in ASP.NET Core

I know that is too late for answering this question, but for whom facing this. The IIS reset pool every 20 minutes and every 20 mins ASP.NET generate new key for protect cookie values (Authentication and Session). to prevent this, add following code to ConfigureServices in Startup class services.AddDataProtection() .PersistKeysToFileSystem(new System.IO.DirectoryInfo(“SOME WHERE IN STORAGE”)) //.ProtectKeysWithCertificate(new … Read more

Restrict route to controller namespace in ASP.NET Core

Update: I’ve found solution through using ActionConstraint. You have to add custom Action Constraint attribute about duplicate actions. Example with duplicate Index methods. First HomeController namespace WebApplication.Controllers { public class HomeController : Controller { [NamespaceConstraint] public IActionResult Index() { return View(); } } } Second HomeController namespace WebApplication { public class HomeController : Controller { … Read more

Windows authentication in asp.net 5

Mark’s answer is still valid in ASP.Net RC1. There are some additional steps to tie it all together (I don’t have enough reputation to comment on his solution): Install WebListener from NuGet Add the following usings to Startcup.cs: using Microsoft.AspNet.Http.Features; using Microsoft.Net.Http.Server; Add Mark’s code snippet in the Configure method before app.UseMvc: // If we’re … Read more

How to manually decrypt an ASP.NET Core Authentication cookie?

Decrypting the Authentication Cookie without needing the keys It’s worth noting that you don’t need to gain access to the keys to decrypt the authentication cookie. You simply need to use the right IDataProtector created with the right purpose parameter, and subpurpose parameters. Based on the CookieAuthenticationMiddleware source code https://github.com/aspnet/Security/blob/rel/1.1.1/src/Microsoft.AspNetCore.Authentication.Cookies/CookieAuthenticationMiddleware.cs#L4 it looks like the purpose … Read more

How to use an Area in ASP.NET Core

In order to include an Area in an ASP.NET Core app, first we need to include a conventional route in the Startup.cs file (It’s best to place it before any non-area route): In Startup.cs/Configure method: app.UseMvc(routes => { routes.MapRoute(“areaRoute”, “{area:exists}/{controller=Admin}/{action=Index}/{id?}”); routes.MapRoute( name: “default”, template: “{controller=Home}/{action=Index}/{id?}”); }); Then make a folder named Areas in the app … Read more

tech