예제 #1
0
        // ReSharper disable once UnusedMember.Global
        public async Task Invoke(HttpContext context, AuthenticationService authService,
                                 SessionCookieService sessionCookieService, UserService userService)
        {
            context.SetSession(new RequestSession());

            string sessionKey = sessionCookieService.GetSessionKey();

            if (string.IsNullOrWhiteSpace(sessionKey))
            {
                await this.next.Invoke(context);

                return;
            }

            var session = await authService.GetSessionBySessionKey(sessionKey);

            var now = DateTime.Now;

            if (session == null || session.LoggedOut || session.ExpirationDate <= now)
            {
                await this.next.Invoke(context);

                return;
            }

            var pocoUser = await userService.GetUserById(session.UserId);

            context.SetSession(new RequestSession
            {
                IsLoggedIn  = true,
                SessionId   = session.LoginSessionId,
                UserAccount = new AccountModel
                {
                    Avatar   = pocoUser.AvatarUrl,
                    UserId   = pocoUser.UserId,
                    Username = pocoUser.Name
                }
            });

            var identity = new ClaimsIdentity("Custom");

            identity.AddClaim(new Claim(ClaimTypes.Name, pocoUser.Name));
            context.User = new ClaimsPrincipal(identity);

            await this.next.Invoke(context);
        }
예제 #2
0
 public AuthController(AuthenticationService authService, SessionService sessionService, SessionCookieService sessionCookieService)
 {
     this.AuthService          = authService;
     this.SessionService       = sessionService;
     this.SessionCookieService = sessionCookieService;
 }