Asp.net vNext Cookie身份验证 [英] Asp.net vNext Cookie Authentication

查看:275
本文介绍了Asp.net vNext Cookie身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的网站需要对第三方Web服务进行身份验证,并创建一个cookie。我们并不需要存储的会员信息。我在Startup.cs以下code

Our website needs to authenticate against a third party webservice and create a cookie. We don't need to store the membership information. I have the following code in 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");

这是行不通的。是否有人可以帮助。

This is not working. Can someone please help.

推荐答案

我觉得在新的beta版本4的东西改变。我会尝试这个code正在我的我的示例应用程序

I think something change on the new beta 4 version. I will try this code is working on my my sample application

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

这篇关于Asp.net vNext Cookie身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆