当调用方法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)
}
});
答案 0 :(得分:0)
看起来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);