Asp.net vNext Cookie Authentication

Our website must authenticate with a third party web service and create a cookie. We don't need to store 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);
        });

      

and login method

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 does not work. Can someone help.

+3


source to share


1 answer


I think that something will change in the new version of beta 4. I will try this code working on my example 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));

      

0


source







All Articles