Asp.net vNext Cookie身份验证

Asp.net vNext Cookie身份验证,asp.net,cookies,asp.net-core,Asp.net,Cookies,Asp.net Core,我们的网站需要针对第三方Web服务进行身份验证并创建cookie。我们不需要存储会员信息。我在Startup.cs中有以下代码 app.UseCookieAuthentication(options => { options.AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.LoginPath = new PathString("

我们的网站需要针对第三方Web服务进行身份验证并创建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");

这是行不通的。有人能帮忙吗

我认为新的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));

您使用的软件包版本是什么?如果您可以看到options.AutomaticAuthentication属性,您应该将其设置为true。请详细说明“不工作”的含义。它与options.AutomaticAuthentication=true一起工作,如@Suhas Joshi所述!