我已经看到了这个主题中的其他问题,但似乎我错过了一些我无法从其他问题中找到的东西。也许它与RavenDb.AspNet.Identity有关。无论如何,我无法看到我的代码中哪些地方出错了。
当我注册用户时。将IdentityUserByUserName
和ApplicationUser
保存到RavenDb时,一切顺利。但是当它转到登录用户await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
时出现问题。并抛出错误:"未找到UserId"。
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
//from AccountController start
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Id = "RavenDbUsers/" + model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
await AsyncDataSession.SaveChangesAsync();
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
//from AccountController end
public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
{
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
}
public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
{
return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
}
}
更新:
// ctor,SingInManager,Controller的UserManager
private ApplicationSignInManager _signInManager;
private UserManager<ApplicationUser> _userManager;
public AccountController()
{
_userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(() => AsyncDataSession));
}
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set
{
_signInManager = value;
}
}
public UserManager<ApplicationUser> UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
// UPDATE:ApplicationUserManager
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var session = context.Get<IAsyncDocumentSession>();
session.Advanced.UseOptimisticConcurrency = true;
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(session));
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
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;
}
答案 0 :(得分:1)
RavenDB.AspNet.Identity的作者。
当你到达这一行时,user.Id的价值是什么?
serial: 1
此外,您可以在执行该代码之前验证用户是否确实存在于数据库中?
我的通灵调试技巧告诉我你的UserManager可能正在使用与accountController.AsyncDataSession不同的IAsyncDocumentSession,因此,当你进行SignInAsync调用时,用户实际上并不在数据库中。