【问题标题】:DotVVM Authentication get null exceptionDotVVM 身份验证获取空异常
【发布时间】:2016-09-09 00:08:03
【问题描述】:

当调用方法 SignIn 时出现错误 NullReferenceExepction。

这是我的 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)
            }
        });

【问题讨论】:

    标签: c# dotvvm


    【解决方案1】:

    看起来 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);
    

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 2018-12-10
      • 2012-01-07
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多