无法使用Cookie身份验证执行登录

时间:2019-03-22 14:22:16

标签: asp.net-identity cookie-authentication

我正在尝试登录,HttpContext.User.Identitiy.IsAuthenticated始终为假。

配置服务

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(
                options => {

                    options.LoginPath = "/";
                    options.AccessDeniedPath =new PathString("/AccessDenied");
                    options.Events.OnRedirectToLogin = (context) => {
                        context.Response.StatusCode = 401;
                        return Task.CompletedTask;
                    };
                });

方法

public async Task Invoke(HttpContext context) {
            string token = context.Request.Query["token"];
            var claims = new List<Claim> {
                new Claim("token",token,APPLICATION_NAME)
            };

            var claimsIdentity = new ClaimsIdentity(claims,CookieAuthenticationDefaults.AuthenticationScheme);
            var authProperties = new AuthenticationProperties {
                AllowRefresh = true,
                ExpiresUtc = DateTimeOffset.Now.AddSeconds(20),
                IsPersistent = true
            };

            await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity),authProperties);
            if (context.User.Identity.IsAuthenticated) { //always false
            }
}

1 个答案:

答案 0 :(得分:1)

SignInAsync不会更改当前请求的用户主体。检查下一个请求的相同属性,该属性应为true

相关问题