将方法添加到ApplicationUserManager



第一次使用asp.net身份,我可能会错过一些东西。我知道如何使用applicationusermanager(我的类扩展UserManager),但是我想创建一种使用UserManager方法的方法,因为我不想重复代码。称"基础"不起作用。

编辑:"基础"不起作用,因为我的方法是静态的(我不知道为什么写这句话)。现在它没有给我错误,但是如果我尝试从我的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();
        }

    }
}

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

相关内容

  • 没有找到相关文章

最新更新