MVC6 (ASP.NET5) 认证 (Asp.net identity) cookie模式 自定义认证

1、Startup类的Configure方法中, 

app.UseIdentity();

改为

app.UseCookieAuthentication(options =>
            { 
                options.AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.AutomaticAuthenticate = true;
                options.AutomaticChallenge = true; 
                options.CookieHttpOnly = true; 
            });

2、登录的Action方法中:

1             List<Claim> claims = new List<Claim>();
2             claims.Add(new Claim("Name", "TestName", ClaimValueTypes.String));
3             claims.Add(new Claim(ClaimTypes.Name, "TestName", ClaimValueTypes.String));
4             ClaimsIdentity identity = new ClaimsIdentity(claims, "AuthenticationType", "Name", ClaimTypes.Role);
5             ClaimsPrincipal principal = new ClaimsPrincipal(identity);
6             HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
7             return View();

 

posted @ 2015-12-09 07:44  乁卬杨  阅读(1606)  评论(1编辑  收藏  举报