2017-09-26 8 views
-1

初めてASP.NET IDを使用すると、何かが紛れている可能性があります。 私は、UserManagerを拡張している私のクラスであるApplicationUserManagerの使い方を知っていますが、コードを繰り返したくないのでUserManagerメソッドを使用するメソッドを作成します。 "base"を呼び出しても機能しません。ApplicationUserManagerにメソッドを追加

EDIT:私が静的なメソッドを持っていたため、「ベース」が機能しませんでした(なぜ私がそれを書いたのかわかりません)。 これでエラーは表示されませんが、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; 
      } 
     } 
    } 
} 

ウェブ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を削除しました(私は他のプロジェクトのすべてのクラスを持っています)、正しいものを参照しました。

関連する問題