2

이것은 this question의 연속입니다.MVC5 (VS2012) UserManager가 사용자에게 로그인하지 않습니다.

나는 userManager 오버라이드 (override)하는 경우 :이 (로그인 과정이 항상 false를 반환합니다 @ Request.IsAuthenticated 제외한 일), 오류가 발생하지 않지만에서 사용자를 기록하지 않습니다

public class NHibernateAspnetUserManager<TUser> : UserManager<TUser> where TUser : IdentityUser 
{ 
    public NHibernateAspnetUserManager(IUserStore<TUser> store) : base(store) 
    { 
    } 

    public override Task<ClaimsIdentity> CreateIdentityAsync(TUser user, string authenticationType) 
    { 
     var identity = new ClaimsIdentity(); 
     identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName)); 
     return Task.FromResult(identity); 
    } 
} 

합니다. 만약 내가 그것을 무시하지 않으면 "System.Security.Claims.Claim..ctor"오류가 다른 질문에서 설명한대로 얻을. 내 자신의 사용자 저장소가 IUserClaimStore를 구현했지만 단순히 새로운 주장 목록을 반환한다는 것을 해결하려고 시도합니다.

기본 사용자 관리자가 다른 후드에서 수행하는 작업에 대해 확신 할 수 없습니다. 나는

var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);    
AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent}, identity); 

편집 할 수 있습니다.이 로그인으로 MVC가 사람을 인식 할 수 있습니다 주장 신분 객체의 형태를 설정 추측하고있는 ctor에 오류가 발생하는 이유

이 발견했다. 사용자 개체가 ID없이 되돌아 왔으므로 기본 UserManager가 당황했습니다. 이 문제를 해결하고 더 이상 오류를 발생시키지 않지만 여전히 사용자를 로그인하지 않는 기본 UserManager를 사용했습니다. 반환하는 ID 객체는 내가 알 수있는 것으로부터 훌륭하게 보입니다.

멀어져 주 그래서 내가 VS2013를 설치하고 모든 처음을 열심히 걸쳐 저장 및 자 NHibernate의 repo를 복사. VS2012에서 MVC5를 업데이트하고 VS2013에서 MVC5를 업데이트하는 것 사이에는 약간의 차이점이 있다고 가정 할 수 있습니다.

답변

0

그래서 주요 문제는 당신이 당신의 방법으로 인증 유형을 존중하지 않는 것입니다, 당신은 DefaultAuthenticationType.ApplicationCookie에 대한 ClaimsIdentity을 작성해야합니다, 여기에 기본 주장 공장이하는 작업은 다음과 같습니다

public override Task<ClaimsIdentity> CreateIdentityAsync(TUser user, string authenticationType) 
    { 
     var id = new ClaimsIdentity(authenticationType, UserNameClaimType, RoleClaimType); 
     id.AddClaim(new Claim(UserIdClaimType, ConvertIdToString(user.Id), ClaimValueTypes.String)); 
     id.AddClaim(new Claim(UserNameClaimType, user.UserName, ClaimValueTypes.String)); 
0

내가 ' ASP.NET 4.5를 사용하여 사용자 지정 ID를 구현하는 것과 동일한 문제에 직면했습니다. 그리고 문제는 사실 nulls 값을 Claims 컬렉션에 추가하는 것입니다 (주석 참조) :

[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Register(RegisterViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     var user = new AppUser { UserName = model.UserName }; 
     // after the UserManager creates a user, all the properties of 
     // AppUser except "UserName" are automatically discarded    
     var result = await UserManager.CreateAsync(new AppUser 
      { 
       UserRealName = model.UserRealName, 
       UserName = model.UserName, 
       Password = model.Password 
      }, model.Password); 
     if (result.Succeeded) 
     { 
      // So we need to re-get the new user 
      user = AppUser.GetByName(model.UserName); 
      await SignInAsync(user, false); // otherwise here we will add null values ... 
      return RedirectToAction("Index", "Home"); 
     } 
     AddErrors(result); 
    } 
    return View(model); 
} 

private async Task SignInAsync(AppUser user, Boolean isPersistent) 
{ 
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); 
    var identity = await UserManager.CreateIdentityAsync(user, 
     DefaultAuthenticationTypes.ApplicationCookie); 
    AuthenticationManager.SignIn(new AuthenticationProperties // ... into the list of 
    // claims for all AppUser properties except UserName 
     { IsPersistent = isPersistent }, identity); 
}