Gcobani Mkontwana

Gcobani Mkontwana

  • 566
  • 1.9k
  • 406k

HttpContext.GetOwinContext() does not contain definition for

Feb 27 2020 11:06 PM
Hi Team
 
I have this error and dont know how to fix it, please help me. 
  1. // ManageController.cs  
  2.   
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.     using System.Linq;  
  6.     using System.Web;  
  7.     using System.Web.Mvc;  
  8.     using System.Threading.Tasks;  
  9.     using Microsoft.AspNet.Identity;  
  10.     using Microsoft.AspNet.Identity.Owin;  
  11.     using Microsoft.Owin.Security;  
  12.     using eNtsaTrainingRegistration.Models;  
  13.   
  14.     namespace eNtsaTrainingRegistration.Controllers  
  15.     {  
  16.         [Authorize]  
  17.         public class ManageController : AppBaseController  
  18.         {  
  19.             private ApplicationSignInManager _signInManager;  
  20.             private ApplicationUserManager _userManager;  
  21.             private ApplicationRoleManager _roleManager;  
  22.   
  23.             // default constructor.  
  24.   
  25.             public ManageController()  
  26.             {  
  27.   
  28.             }  
  29.             public ManageController(ApplicationSignInManager signInManager, ApplicationUserManager userManager, ApplicationRoleManager roleManager)  
  30.             {  
  31.                 UserManager = userManager;  
  32.                 SignInManager = signInManager;  
  33.                 AppRoleManager = roleManager;  
  34.             }  
  35.             public ApplicationUserManager UserManager  
  36.             {  
  37.                 get  
  38.                 {  
  39.                     return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>()// Error is here;  
  40.                 }  
  41.                 private set  
  42.                 {  
  43.                     _userManager = value;  
  44.                 }  
  45.             }  
  46.   
  47.             public ApplicationRoleManager AppRoleManager  
  48.             {  
  49.                 get  
  50.                 {  
  51.                     return _roleManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationRoleManager>();  
  52.                 }  
  53.             }  
  54.   
  55.   
  56.         }  
  57.   
  58.   
  59.     }  
  60.   
  61. //IdentityConfig.cs  
  62.   
  63.     using System;  
  64.     using System.Collections.Generic;  
  65.     using System.Linq;  
  66.     using System.Web;  
  67.     using System.Data.Entity;  
  68.     using System.Security.Claims;  
  69.     using System.Threading.Tasks;  
  70.     using Microsoft.AspNet.Identity;  
  71.     using Microsoft.AspNet.Identity.EntityFramework;  
  72.     using Microsoft.AspNet.Identity.Owin;  
  73.     using Microsoft.Owin;  
  74.     using Microsoft.Owin.Security;  
  75.     using System.Net.Mail;  
  76.     using System.Net;  
  77.     using System.Web.Configuration;  
  78.     using eNtsaTrainingRegistration.Models;  
  79.   
  80.     namespace eNtsaTrainingRegistration  
  81.     {  
  82.         public class EmailService : IIdentityMessageService  
  83.         {  
  84.             public Task SendAsync(IdentityMessage message)  
  85.             {  
  86.                 var mailMessage = new MailMessage();  
  87.                 mailMessage.To.Add(new MailAddress(message.Destination));  
  88.                 mailMessage.From = new MailAddress("Gcobani Mkontwana <[email protected]>");  
  89.                 mailMessage.Subject = message.Subject;  
  90.                 mailMessage.IsBodyHtml = true;  
  91.                 mailMessage.Body = message.Body;  
  92.   
  93.                 using (var smtp = new SmtpClient())  
  94.                 {  
  95.                     var credential = new NetworkCredential  
  96.                     {  
  97.                         UserName = WebConfigurationManager.AppSettings["UserName"],  
  98.                         Password = Helper_b.Decrypt(WebConfigurationManager.AppSettings["UserPassword"])  
  99.                     };  
  100.                     smtp.Credentials = credential;  
  101.                     smtp.Host = WebConfigurationManager.AppSettings["SMTPName"];  
  102.                     smtp.Port = int.Parse(WebConfigurationManager.AppSettings["SMTPPort"]);  
  103.                     smtp.EnableSsl = true;  
  104.                     smtp.Send(mailMessage);  
  105.                 }  
  106.                 return Task.FromResult(0);  
  107.             }  
  108.         }  
  109.   
  110.         public class SmsService : IIdentityMessageService  
  111.         {  
  112.             public Task SendAsync(IdentityMessage message)  
  113.             {  
  114.                 return Task.FromResult(0);  
  115.             }  
  116.         }  
  117.         // add another method here.  
  118.         public class ApplicationUserManager : UserManager<ApplicationUser>  
  119.         {  
  120.             public ApplicationUserManager(IUserStore<ApplicationUser> store)  
  121.                 : base(store)  
  122.             {  
  123.             }  
  124.   
  125.             public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)  
  126.             {  
  127.                 var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));  
  128.                 // Configure validation logic for usernames  
  129.                 manager.UserValidator = new UserValidator<ApplicationUser>(manager)  
  130.                 {  
  131.                     AllowOnlyAlphanumericUserNames = false,  
  132.                     RequireUniqueEmail = true  
  133.                 };  
  134.   
  135.                 // Configure validation logic for passwords  
  136.                 manager.PasswordValidator = new PasswordValidator  
  137.                 {  
  138.                     RequiredLength = 6,  
  139.                     RequireNonLetterOrDigit = true,  
  140.                     RequireDigit = true,  
  141.                     RequireLowercase = true,  
  142.                     RequireUppercase = true,  
  143.                 };  
  144.   
  145.                 // Configure user lockout defaults  
  146.                 manager.UserLockoutEnabledByDefault = true;  
  147.                 manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);  
  148.                 manager.MaxFailedAccessAttemptsBeforeLockout = 5;  
  149.   
  150.                 // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user  
  151.                 // You can write your own provider and plug it in here.  
  152.                 manager.RegisterTwoFactorProvider("Phone Code"new PhoneNumberTokenProvider<ApplicationUser>  
  153.                 {  
  154.                     MessageFormat = "Your security code is {0}"  
  155.                 });  
  156.                 manager.RegisterTwoFactorProvider("Email Code"new EmailTokenProvider<ApplicationUser>  
  157.                 {  
  158.                     Subject = "Security Code",  
  159.                     BodyFormat = "Your security code is {0}"  
  160.                 });  
  161.                 manager.EmailService = new EmailService();  
  162.                 manager.SmsService = new SmsService();  
  163.                 var dataProtectionProvider = options.DataProtectionProvider;  
  164.                 if (dataProtectionProvider != null)  
  165.                 {  
  166.                     manager.UserTokenProvider =  
  167.                         new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));  
  168.                 }  
  169.                 return manager;  
  170.             }  
  171.         }  
  172.         // SignInManager class.  
  173.         public class ApplicationSignInManager : SignInManager<ApplicationUser, string>  
  174.         {  
  175.             public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)  
  176.             : base(userManager, authenticationManager)  
  177.             {  
  178.             }  
  179.   
  180.             public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)  
  181.             {  
  182.                 return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);  
  183.             }  
  184.   
  185.             public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)  
  186.             {  
  187.                 return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);  
  188.             }  
  189.         }  
  190.         public class ApplicationRoleManager:RoleManager<IdentityRole>  
  191.         {  
  192.             public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)  
  193.                 :base(roleStore)  
  194.             {  
  195.   
  196.             }  
  197.             public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)  
  198.             {  
  199.                 var appRoleManager = new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));  
  200.   
  201.                 return appRoleManager;  
  202.             }  
  203.         }  
 

Answers (3)