How to fix Can't find Request.GetOwinContext in Web API
By FoxLearn 2/18/2024 1:33:47 AM 43
If you get an error can't get the UserManager from OwinContext in ApiController using ASP.NET MVC
You can fix the problem as below.
using System.Web; using System.Web.Http; using Microsoft.AspNet.Identity.Owin; using System.Linq; using System; using System.Threading.Tasks; using Microsoft.AspNet.Identity; namespace Invoice.Controllers { public class AccountController : ApiController { private ApplicationSignInManager _signInManager; private ApplicationUserManager _userManager; public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager) { UserManager = userManager; SignInManager = signInManager; } public ApplicationSignInManager SignInManager { get { return _signInManager ?? HttpContext.Current.GetOwinContext().Get<ApplicationSignInManager>(); } private set { _signInManager = value; } } public ApplicationUserManager UserManager { get { return _userManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); } private set { _userManager = value; } } } }
You should install Microsoft.AspNet.Identity.Owin from the Nuget Package Manager into your project, then just add the following code to the top.
using Microsoft.AspNet.Identity.Owin; using Microsoft.AspNet.Identity;
Remember replace HttpContext.GetOwinContext().Get<ApplicationSignInManager>() to HttpContext.Current.GetOwinContext().Get<ApplicationSignInManager>()
- How to fix "InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager'"
- How to fix 'IMvcBuilder' does not contain a definition for 'AddNewtonsoftJson'
- ASP.NET Core: How to Create RDLC Report in ASP.NET Core
- ASP.NET MVC: Free Responsive Templates
- How to fix Assets file 'project.assets.json' not found
- How to fix 'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer'
- How to set cookie ValidationInterval in ASP.NET Core
- How to fix LoginPath not working in ASP.NET Core
Categories
Popular Posts
Visual Studio: Auto increment version
04/01/2024
What is ARM architecture?
04/01/2024