private bool AuthenticateSession(string userName, string password, string hash, ref AuthenticatedSessionInfo sesInfo)
        {
            sesInfo = null;
            var userService    = _injector.Resolve <IStreamingUserService>();
            var sessionService = _injector.Resolve <IStreamingUserSessionService>();

            //userService.CreateUser("zqrtalent", "zqrtalentpass");

            Guid userId;

            if (userService.Authenticate(userName, password, hash, out userId))
            {
                var sInfo = sessionService.StartSession(userId, Request.Host.Value);
                if (sInfo != null)
                {
                    sesInfo = new AuthenticatedSessionInfo
                    {
                        SessionKey     = sInfo.SessionKey,
                        UserId         = userId,
                        UserName       = userName,
                        PlayingMediaId = sInfo.PlayingMediaId,
                        PlayingAtMSec  = sInfo.PlayingAtMSec
                    };
                }
            }
            return(sesInfo != null);
        }
        private AuthenticationTicket CreateAuthTicket(AuthenticatedSessionInfo sessionInfo)
        {
            var claims = new []
            {
                new Claim(ClaimTypes.Name, sessionInfo.UserName),
                new Claim(ClaimTypes.NameIdentifier, sessionInfo.UserId.ToString()),
            };
            var claimsIdentity = new ClaimsIdentity(claims, Options.Scheme);
            var principal      = new ClaimsPrincipal(new [] { claimsIdentity });

            return(new AuthenticationTicket(principal, new AuthenticationProperties(), Options.Scheme));
        }
        private void SetAuthenticatedInfo(string sessionKey, AuthenticatedSessionInfo sessInfo)
        {
            string infoJson = JsonConvert.SerializeObject(sessInfo);
            var    cache    = _injector.Resolve <IMemoryCache>();

            if (Options.Expiration != null)
            {
                cache.Set(sessionKey, infoJson, Options.Expiration);
            }
            else
            {
                cache.Set(sessionKey, infoJson);
            }
        }
        private bool GetAthenticatedInfo(string sessionKey, out AuthenticatedSessionInfo sessInfo)
        {
            var    cache    = _injector.Resolve <IMemoryCache>();
            string infoJson = string.Empty;

            sessInfo = null;

            if (cache.TryGetValue(sessionKey, out infoJson))
            {
                sessInfo = JsonConvert.DeserializeObject <AuthenticatedSessionInfo>(infoJson);
            }

            return(sessInfo != null);
        }
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (string.IsNullOrEmpty(Options.Scheme) || string.IsNullOrEmpty(Options.AccessTokenKeyHeaderName))
            {
                throw new ArgumentException($"Make sure to configure {nameof(MyAuthSchemeOptions)} options!");
            }

            StringValues values;

            if (Request.Headers.TryGetValue("Authorization", out values) && values.Count == 1)
            {
                var arrValues = values[0].Split(" ");
                if (arrValues[0].ToLower() != "basic")
                {
                    return(AuthenticateResult.Fail("Invalid authorization header!"));
                }

                string userName, password, hash;
                if (!ParseCridentials(arrValues[1], out userName, out password, out hash))
                {
                    return(AuthenticateResult.Fail("Invalid authorization header!"));
                }

                AuthenticatedSessionInfo sesInfo = null;
                if (AuthenticateSession(userName, password, hash, ref sesInfo))
                {
                    Request.HttpContext.Items.Add(Options.SessionInfoContextKeyName, sesInfo);
                    SetAuthenticatedInfo(sesInfo.SessionKey, sesInfo);

                    return(await Task.FromResult(AuthenticateResult.Success(CreateAuthTicket(sesInfo))));
                }
            }
            else if (Request.Headers.TryGetValue(Options.AccessTokenKeyHeaderName, out values) && values.Count > 0)
            {
                AuthenticatedSessionInfo sesInfo = null;
                if (GetAthenticatedInfo(values[0], out sesInfo))
                {
                    Request.HttpContext.Items.Add(Options.SessionInfoContextKeyName, sesInfo);
                    return(await Task.FromResult(AuthenticateResult.Success(CreateAuthTicket(sesInfo))));
                }
                else
                {
                    return(await Task.FromResult(AuthenticateResult.Fail("Invalid auth token!")));
                }
            }

            return(await Task.FromResult(AuthenticateResult.Fail("Unknown error occured while trying to authenticate!")));
        }