2016-09-07 3 views
0

메소드 SignIn을 호출 할 때 NullReferenceExpression 오류가 발생합니다.DotVVM 인증에서 null 예외가 발생합니다.

public class UserIdentity : IIdentity 
{ 
    public string AuthenticationType 
    { 
     get { return DefaultAuthenticationTypes.ApplicationCookie; } 
    } 

    public bool IsAuthenticated { get; set; } 

    public string Name { get; private set; } 

    public UserIdentity(string name) 
    { 
     Name = name; 
    } 
} 

이 또한 내가 Startup.cs에 추가 : 다음 ClaimsIdentity은 마치

app.UseCookieAuthentication(new CookieAuthenticationOptions() 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      Provider = new CookieAuthenticationProvider() 
      { 
       OnApplyRedirect = e => DotvvmAuthenticationHelper.ApplyRedirectResponse(e.OwinContext, e.RedirectUri) 
      } 
     }); 

답변

0

다음
public Masterpage1ViewModel() { 
     UserIdentity user = new UserIdentity("Admin"); 
     var claimsIdentity = new ClaimsIdentity(user); 

     Context.OwinContext.Authentication.SignIn(claimsIdentity); 
    } 

이 UserIdentity을위한 클래스입니다 : 여기

내 뷰 모델입니다 정확하게 초기화되지 않았습니다. UserIdentity과의 조합이 어딘가에서 충돌 할 수 있습니다. 사이드 오윈 쿠키 보안 미들웨어.

우리는 초기화하려면 다음 코드를 사용합니다

var identity = new ClaimsIdentity(new[] 
{ 
    new Claim(ClaimTypes.Name, UserName), 
    // add another claims (e.g. for each role) 
}, 
CookieAuthenticationDefaults.AuthenticationType); 
Context.OwinContext.Authentication.SignIn(identity);