我们的网站需要针对第三方网络服务进行身份验证并创建Cookie。我们不需要存储会员信息。我在Startup.cs中有以下代码
app.UseCookieAuthentication(options => {
options.AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.LoginPath = new PathString("/User/Login");
options.CookieName = "GEMSNCID";
options.ExpireTimeSpan = new System.TimeSpan(1, 0, 0);
});
,登录方法是
var claims = new[]
{
new Claim(ClaimTypes.Name, model.UserName),
new Claim(ClaimTypes.Country, "USA")
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
ClaimsPrincipal principal = new ClaimsPrincipal(identity);
Context.Response.SignIn(CookieAuthenticationDefaults.AuthenticationScheme, principal);
return RedirectToAction("Index", "Home");
这不起作用。有人可以请帮助。
答案 0 :(得分:0)
我认为新的beta 4版本会有所改变。我将尝试此代码正在处理我的示例应用程序
var claims = new[]
{
new Claim(ClaimTypes.Name, model.UserName),
new Claim(ClaimTypes.Country, "USA")
};
var user = this.User as ClaimsPrincipal;
var identity = user.Identities.Where(x => x.AuthenticationType == CookieAuthenticationDefaults.AuthenticationScheme).FirstOrDefault();
if (identity == null)
{
identity = new ClaimsIdentity(claims.ToArray(), CookieAuthenticationDefaults.AuthenticationScheme);
user.AddIdentity(identity);
}
else
identity.AddClaims(claims);
if (this.Context.Response.HttpContext.User.Claims.Count() > 1)
this.Context.Response.SignIn(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));