Beispiel #1
0
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            // build the claims and put them in "Context"; you need to import the Microsoft.AspNetCore.Authentication package
            if (Request.Headers.ContainsKey("Authorization"))
            {
                //Authorization header not in request
                if (!AuthenticationHeaderValue.TryParse(Request.Headers["Authorization"], out AuthenticationHeaderValue headerValue))
                {
                    //Invalid Authorization header
                    return(AuthenticateResult.Fail("invalid Auth Header"));
                }
                AppTokenHandler TokenHdlr      = new AppTokenHandler();
                var             tokenPrincipal = await TokenHdlr.ValidateJWTToken(headerValue.Parameter);

                if (tokenPrincipal != null)
                {
                    var ticket = new AuthenticationTicket(tokenPrincipal, Scheme.Name);
                    return(AuthenticateResult.Success(ticket));
                }
                else
                {
                    return(AuthenticateResult.Fail("invalid token"));
                }
            }


            return(AuthenticateResult.NoResult());
        }
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            //check the cookies present
            if (Request.Cookies["LMSAuthCookies"] != null)
            {
                string TempCookiesValue = Request.Cookies["LMSAuthCookies"];
                try
                {
                    var TempCookiesClaim = _AppEncription.ConvertBase64StringToObject <List <AppKeyValueInfo> >(TempCookiesValue);
                    if (TempCookiesClaim != null || TempCookiesClaim.Count == 0)
                    {
                        return(AuthenticateResult.Fail("Invalid Token."));
                    }

                    var AccessToken = TempCookiesClaim.FirstOrDefault(x => x.Key == "JWTToken")?.Value;
                    if (!string.IsNullOrWhiteSpace(AccessToken))
                    {
                        AppTokenHandler TokenHdlr    = new AppTokenHandler();
                        var             IsValidToken = await TokenHdlr.ValidateJWTToken(AccessToken);

                        if (IsValidToken != null)
                        {
                            var principal = new ClaimsPrincipal(Request.HttpContext.User.Identity);
                            var ticket    = new AuthenticationTicket(principal, Scheme.Name);
                            return(AuthenticateResult.Success(ticket));
                        }
                        else
                        {
                            return(AuthenticateResult.Fail("Token not valid or expire"));
                        }
                    }
                    else
                    {
                        return(AuthenticateResult.Fail("Token not found"));
                    }
                }
                catch (Exception)
                {
                    return(AuthenticateResult.Fail("Invalid Token."));
                }
            }

            return(AuthenticateResult.NoResult());
        }