首页 新闻 会员 周边

.netcore登录遇到问题 No authentication handler is configured to handle the scheme: Cookies

0
悬赏园豆:10 [已解决问题] 解决于 2016-07-18 11:24
            app.UseCookieAuthentication(new CookieAuthenticationOptions()
            {
                AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme,
                LoginPath = new PathString("/Account/Unauthorized/"),
                AccessDeniedPath = new PathString("/Account/Forbidden/"),
                AutomaticAuthenticate = true,
                AutomaticChallenge = false,
                CookieHttpOnly = true,
                CookieName = "MyCookie",
                ExpireTimeSpan = TimeSpan.FromHours(2)
            });
        public async void Login()
        {
            if (!HttpContext.User.Identities.Any(identity => identity.IsAuthenticated))
            {
                var user = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "bob") }, CookieAuthenticationDefaults.AuthenticationScheme));
                await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user);

                HttpContext.Response.ContentType = "text/plain";
                await HttpContext.Response.WriteAsync("Hello First timer");
            }
            else
            {
                HttpContext.Response.ContentType = "text/plain";
                await HttpContext.Response.WriteAsync("Hello old timer");
            }
        }

高手帮忙解答

痛快的主页 痛快 | 初学一级 | 园豆:71
提问于:2016-07-16 23:01
< >
分享
最佳答案
0

将 AutomaticChallenge 改为 true 试试。

收获园豆:10
dudu | 高人七级 |园豆:30994 | 2016-07-17 08:10

将 AutomaticChallenge 改为 true 后一样报错,改为public async Task<bool> Login() 且不报错,但是Cookies还是写不进去,身份认证无效

痛快 | 园豆:71 (初学一级) | 2016-07-18 10:14
其他回答(1)
0

问题已解决,参考 MusicStore-dev

痛快 | 园豆:71 (初学一级) | 2016-07-18 11:23

怎么解决的,能不能说一下?

支持(0) 反对(0) 夕阳茶 | 园豆:192 (初学一级) | 2016-08-08 11:52

@夕阳茶: 就是把Cookies设置放到 MVC之前

 app.UseCookieAuthentication(new CookieAuthenticationOptions()
            {
                AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme,
                LoginPath = new PathString("/Account/Unauthorized/"),
                AccessDeniedPath = new PathString("/Account/Forbidden/"),
                AutomaticAuthenticate = true,
                AutomaticChallenge = false,
                CookieHttpOnly = true,
                CookieName = "MyCookie",
                ExpireTimeSpan = TimeSpan.FromHours(2)
            });
 app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
支持(0) 反对(0) 痛快 | 园豆:71 (初学一级) | 2016-08-08 16:10

@痛快: 嗯,我在官方的文档上也找到了答案,谢谢。https://docs.asp.net/en/latest/security/authentication/cookie.html

支持(0) 反对(0) 夕阳茶 | 园豆:192 (初学一级) | 2016-08-08 16:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册