2017-09-26 106 views
-1

我第一次使用ASP.NET身份,我可能会错过一些东西。 我知道如何使用ApplicationUserManager(我的类扩展UserManager),但我想创建一个使用UserManager方法的方法,因为我不想重复代码。 调用“base”不起作用。将方法添加到ApplicationUserManager

编辑:“基地”没有工作,因为我有方法为静态(我不知道为什么我写了)。 现在它不会给我错误,但如果我尝试从我的Web API控制器调用它,我会得到“不包含...的定义...”错误。

ApplicationUserManager:

namespace BLL 
{ 
    // Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application. 
    public class ApplicationUserManager : UserManager<ApplicationUser> 
    { 
     public ApplicationUserManager(IUserStore<ApplicationUser> store) 
      : base(store) 
     { 
     } 

     public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
     { 
      var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())); 
      // Configure validation logic for usernames 
      manager.UserValidator = new UserValidator<ApplicationUser>(manager) 
      { 
       AllowOnlyAlphanumericUserNames = false, 
       RequireUniqueEmail = true 
      }; 

      // Configure validation logic for passwords 
      manager.PasswordValidator = new PasswordValidator 
      { 
       RequiredLength = 6, 
       RequireNonLetterOrDigit = false, 
       RequireDigit = false, 
       RequireLowercase = false, 
       RequireUppercase = false, 
      }; 

      // 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<ApplicationUser> 
      { 
       MessageFormat = "Your security code is {0}" 
      }); 
      manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser> 
      { 
       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<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity")); 
      } 
      return manager; 
     } 

     public async Task<int> RegistraPuntoScan(string userId, string sitoVisitato) 
     { 
      var user = await base.FindByIdAsync(userId); 
      if(user != null) 
      { 
       var s = new Stringa(sitoVisitato); 
       if (!user.URLVisitati.Contains(s)) 
       { 
        user.Punti++; 
        user.URLVisitati.Add(s); 
        await base.UpdateAsync(user); 
        return 1; 
       } 
       else 
       { 
        return 2; 
       } 
      } 
      else 
      { 
       return 3; 
      } 
     } 
    } 
} 

的Web API控制器:

namespace MyProject.Controllers.API 
{ 
    [CustomAuthorization] 
    public class PuntiController : ApiController 
    { 
     private ApplicationUserManager _userManager; 

     public ApplicationUserManager UserManager 
     { 
      get 
      { 
       return _userManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
      } 
      private set 
      { 
       _userManager = value; 
      } 
     } 

     [HttpPost] 
     public IHttpActionResult RegistraPuntoScan(RegisterPointScanVm vm) 
     { 
      ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal; 
      var idUtente = ClaimsPrincipal.Current.Identity.GetUserId(); 
      var user = UserManager.FindById(idUtente); 
      switch(UserManager.RegistraPuntoScan(idUtente, vm.ScannedURL)) 
      { 
       case 1: 
        return Ok(); 
       case 2: 
        return Conflict(); 
       case 3: 
        return BadRequest(); 
      } 
      return BadRequest(); 

     } 


    } 
} 
+0

您尝试使用哪些基本方法? – dcg

+0

我试图用base.FindById(id)(例子)创建一个方法并调用父方法,但它不起作用 –

+0

我相信你应该发布不适合你的代码,以便我们仔细观察。发布ApplicationUserManager定义。 – dcg

回答

0

我解决了这个问题。 我无法调用新方法,因为我在另一个项目中有ApplicationUserManager,我可能忘记删除默认的ApplicationUserManager n IdentityConfig.cs,或VisualStudio创建它,我不知道。 我删除了IdentityConfig(我有其他项目中的所有类)并引用了正确的一个,现在一切正常。