2016-09-07 3 views
0

メソッドSignInが呼び出されると、NullReferenceExepctionエラーが発生します。ここでDotVVM認証でヌル例外が発生する

は私のViewModelである:ここでは

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

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

がUserIdentityためのクラスです:

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に追加:

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

答えて

0

ClaimsIdentityがあるように見えます正しく初期化されていない可能性があります。おそらくUserIdentityの組み合わせがどこかでクラッシュしますサイドのOWIN Cookie Securityミドルウェア。

我々はそれを初期化するために、次のコードを使用します。

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); 
関連する問題