我第一次使用ASP.NET身份,我可能会错过一些东西。 我知道如何使用ApplicationUserManager(我的类扩展UserManager),但我想在其中创建一个使用UserManager方法的方法,因为我不想重复代码。 呼叫" base"不起作用。
编辑:"基地"没有工作,因为我的方法是静态的(我不知道为什么我写这个)。 现在它没有给我错误,但如果我尝试从我的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 Controller:
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 :(得分:0)
我解决了这个问题。 我无法调用新方法,因为我在另一个项目中有ApplicationUserManager,我可能忘了删除默认的ApplicationUserManager n IdentityConfig.cs,或者VisualStudio创建它,我不知道。 我删除了IdentityConfig(我拥有其他项目中的所有类)并引用了正确的类,现在一切正常。