DotVVM鉴权get null异常

本文关键字:异常 null get 鉴权 DotVVM | 更新日期: 2023-09-27 18:06:02

当调用SignIn方法时,我得到错误nullreferenceexception。

这是我的ViewModel:
public Masterpage1ViewModel() {
        UserIdentity user = new UserIdentity("Admin");
        var claimsIdentity = new ClaimsIdentity(user);
        Context.OwinContext.Authentication.SignIn(claimsIdentity);
  }

UserIdentity的class:

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)
            }
        });

DotVVM鉴权get null异常

看起来ClaimsIdentity没有正确初始化,可能与UserIdentity的组合在OWIN Cookie安全中间件中的某个地方崩溃。

使用以下代码初始化它:

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);