0

When is called method SignIn I get error NullReferenceExepction.

Here is my ViewModel:

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

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

Here is class for 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;
    }
}

Also I added to Startup.cs:

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

1 Answers1

0

It looks like the ClaimsIdentity is not initialized correctly, maybe the combination with UserIdentity crashes somewhere inside OWIN Cookie Security Middleware.

We use the following code to initialize it:

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);
Tomáš Herceg
  • 1,595
  • 1
  • 13
  • 18