Hi Team
I have this error and dont know how to fix it, please help me.
- // ManageController.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Threading.Tasks;
- using Microsoft.AspNet.Identity;
- using Microsoft.AspNet.Identity.Owin;
- using Microsoft.Owin.Security;
- using eNtsaTrainingRegistration.Models;
- namespace eNtsaTrainingRegistration.Controllers
- {
- [Authorize]
- public class ManageController : AppBaseController
- {
- private ApplicationSignInManager _signInManager;
- private ApplicationUserManager _userManager;
- private ApplicationRoleManager _roleManager;
- // default constructor.
- public ManageController()
- {
- }
- public ManageController(ApplicationSignInManager signInManager, ApplicationUserManager userManager, ApplicationRoleManager roleManager)
- {
- UserManager = userManager;
- SignInManager = signInManager;
- AppRoleManager = roleManager;
- }
- public ApplicationUserManager UserManager
- {
- get
- {
- return _userManager ?? HttpContext.GetOwinContext().GetUserManager
() // Error is here; - }
- private set
- {
- _userManager = value;
- }
- }
- public ApplicationRoleManager AppRoleManager
- {
- get
- {
- return _roleManager ?? HttpContext.GetOwinContext().GetUserManager
(); - }
- }
- }
- }
- //IdentityConfig.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Data.Entity;
- using System.Security.Claims;
- using System.Threading.Tasks;
- using Microsoft.AspNet.Identity;
- using Microsoft.AspNet.Identity.EntityFramework;
- using Microsoft.AspNet.Identity.Owin;
- using Microsoft.Owin;
- using Microsoft.Owin.Security;
- using System.Net.Mail;
- using System.Net;
- using System.Web.Configuration;
- using eNtsaTrainingRegistration.Models;
- namespace eNtsaTrainingRegistration
- {
- public class EmailService : IIdentityMessageService
- {
- public Task SendAsync(IdentityMessage message)
- {
- var mailMessage = new MailMessage();
- mailMessage.To.Add(new MailAddress(message.Destination));
- mailMessage.From = new MailAddress("Gcobani Mkontwana
" ); - mailMessage.Subject = message.Subject;
- mailMessage.IsBodyHtml = true;
- mailMessage.Body = message.Body;
- using (var smtp = new SmtpClient())
- {
- var credential = new NetworkCredential
- {
- UserName = WebConfigurationManager.AppSettings["UserName"],
- Password = Helper_b.Decrypt(WebConfigurationManager.AppSettings["UserPassword"])
- };
- smtp.Credentials = credential;
- smtp.Host = WebConfigurationManager.AppSettings["SMTPName"];
- smtp.Port = int.Parse(WebConfigurationManager.AppSettings["SMTPPort"]);
- smtp.EnableSsl = true;
- smtp.Send(mailMessage);
- }
- return Task.FromResult(0);
- }
- }
- public class SmsService : IIdentityMessageService
- {
- public Task SendAsync(IdentityMessage message)
- {
- return Task.FromResult(0);
- }
- }
- // add another method here.
- public class ApplicationUserManager : UserManager
- {
- public ApplicationUserManager(IUserStore
store) - : base(store)
- {
- }
- public static ApplicationUserManager Create(IdentityFactoryOptions
options, IOwinContext context) - {
- var manager = new ApplicationUserManager(new UserStore
(context.Get ())); - // Configure validation logic for usernames
- manager.UserValidator = new UserValidator
(manager) - {
- AllowOnlyAlphanumericUserNames = false,
- RequireUniqueEmail = true
- };
- // Configure validation logic for passwords
- manager.PasswordValidator = new PasswordValidator
- {
- RequiredLength = 6,
- RequireNonLetterOrDigit = true,
- RequireDigit = true,
- RequireLowercase = true,
- RequireUppercase = true,
- };
- // Configure user lockout defaults
- manager.UserLockoutEnabledByDefault = true;
- manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
- manager.MaxFailedAccessAttemptsBeforeLockout = 5;
- // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
- // You can write your own provider and plug it in here.
- manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider
- {
- MessageFormat = "Your security code is {0}"
- });
- manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider
- {
- Subject = "Security Code",
- BodyFormat = "Your security code is {0}"
- });
- manager.EmailService = new EmailService();
- manager.SmsService = new SmsService();
- var dataProtectionProvider = options.DataProtectionProvider;
- if (dataProtectionProvider != null)
- {
- manager.UserTokenProvider =
- new DataProtectorTokenProvider
(dataProtectionProvider.Create( "ASP.NET Identity")); - }
- return manager;
- }
- }
- // SignInManager class.
- public class ApplicationSignInManager : SignInManager
string> - {
- public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
- : base(userManager, authenticationManager)
- {
- }
- public override Task
CreateUserIdentityAsync(ApplicationUser user) - {
- return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
- }
- public static ApplicationSignInManager Create(IdentityFactoryOptions
options, IOwinContext context) - {
- return new ApplicationSignInManager(context.GetUserManager
(), context.Authentication); - }
- }
- public class ApplicationRoleManager:RoleManager
- {
- public ApplicationRoleManager(IRoleStore
string> roleStore) - :base(roleStore)
- {
- }
- public static ApplicationRoleManager Create(IdentityFactoryOptions
options, IOwinContext context) - {
- var appRoleManager = new ApplicationRoleManager(new RoleStore
(context.Get ())); - return appRoleManager;
- }
- }
Siddharth GajbhiyePosted Feb 28, 2020, 1:05 AM
Guest UserPosted Feb 27, 2020, 11:59 PM
Rajanikant HawaldarPosted Feb 27, 2020, 11:51 PM