Asp.net vNext Cookie身份验证

3
我们的网站需要对接第三方 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");

这个不起作用。可以有人帮忙吗?


2
你使用的软件包版本是什么?如果你能看到 AutomaticAuthentication 属性选项,你应该将其设置为 true。 - Suhas Joshi
请详细说明您所说的“不起作用”是什么意思。 - tugberk
它可以使用选项 AutomaticAuthentication = true,就像 @Suhas Joshi 描述的那样! - Dmitry Sikorsky
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接