dotnetcore / osharp

OSharp是一个基于.Net6.0的快速开发框架,框架对 AspNetCore 的配置、依赖注入、日志、缓存、实体框架、Mvc(WebApi)、身份认证、功能权限、数据权限等模块进行更高一级的自动化封装,并规范了一套业务实现的代码结构与操作流程,使 .Net 框架更易于应用到实际项目开发中。
Apache License 2.0
2.79k stars 752 forks source link

基于cookie的登录问题,User.Identity.IsAuthenticated 仍然是false #117

Closed 491134648 closed 4 years ago

491134648 commented 5 years ago

描述一下BUG A clear and concise description of what the bug is. 当我调用_signInManager.SignInAsync写入登录cookie时,cookie信息是写入成功的,但是请求时Profile中的User.Identity.IsAuthenticated 仍然是false,是我的调用姿势不对吗 重现步骤 Steps to reproduce the behavior:

  1. Go to '...' `[HttpPost] [ModuleInfo] [Description("用户登录")] public async Task GoLogin(LoginDto dto) { IRecaptchaService _recaptcha = null; Check.NotNull(dto, nameof(dto)); if (_recaptcha != null) { dto.IsCaptcha = true; var recaptcha = await _recaptcha.Validate(Request); if (!recaptcha.success) { ModelState.AddModelError("Recaptcha", "验证码错误,请确认重试"); return View(dto); } } dto.Ip = HttpContext.GetClientIp(); dto.UserAgent = Request.Headers["User-Agent"].FirstOrDefault(); OperationResult result = await _identityContract.Login(dto); IUnitOfWork unitOfWork = HttpContext.RequestServices.GetUnitOfWork<User, int>(); unitOfWork.Commit(); if (!result.Succeeded) { ModelState.AddModelError("Account", result.Message); //return View(dto); return RedirectToAction("Login", dto); } User user = result.Data;

        await _signInManager.SignInAsync(user, dto.Remember);
        //var temp = Profile();
        return RedirectToAction("Index", "Home");
    }`

    ` [HttpGet] [ModuleInfo] [Description("用户信息")] public async Task Profile() { var data = HttpContext.User; if (!User.Identity.IsAuthenticated) { return null; }

        IOnlineUserProvider onlineUserProvider = HttpContext.RequestServices.GetService<IOnlineUserProvider>();
        if (onlineUserProvider == null)
        {
            return null;
        }
        OnlineUser onlineUser = await onlineUserProvider.GetOrCreate(User.Identity.Name);
        onlineUser.RefreshTokens.Clear();
    
        return onlineUser;
    }`

    期望行为 User.Identity.IsAuthenticated 应该为true

截图 If applicable, add screenshots to help explain your problem.

附加上下文,如截图 Add any other context about the problem here. image

运行环境

Desktop (please complete the following information):

491134648 commented 5 years ago

已经处理,与jwt冲突了,在cookie中写入jwttoken,在identitypack中解析cookie或session的指定key的值,赋值给context.token

gmf520 commented 5 years ago

框架本身只使用了jwt,还没有考虑cookie的方式,待优化

gmf520 commented 4 years ago

https://github.com/dotnetcore/OSharp/commit/e28cfb726429626d7d254d6666c6581fbfa1c30e