问题
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);
});
and the login method is
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.
回答1:
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));
来源:https://stackoverflow.com/questions/30056234/asp-net-vnext-cookie-authentication