Exemple #1
0
        public static JwtBearerOptions DefaultJwtBearerOptions(this JwtBearerOptions options, AppSettings settings)
        {
            options.RequireHttpsMetadata      = false;
            options.SaveToken                 = true;
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer      = true,
                ValidIssuer         = settings.Issuer,
                ValidateAudience    = true,
                ValidAudience       = settings.Audience,
                RequireSignedTokens = false,
                ClockSkew           = TimeSpan.Zero,
                TokenDecryptionKey  = new X509SecurityKey(new X509Certificate2(settings.PrivateKey, settings.PasswordCertificate))
            };
            options.Events = new JwtBearerEvents
            {
                OnChallenge = context =>
                {
                    context.HandleResponse();
                    context.Response.ContentType = "application/json";
                    context.Response.StatusCode  = 401;
                    var content = new
                    {
                        error       = context.Error,
                        description = context.ErrorDescription,
                        statusCode  = context.Response.StatusCode
                    };
                    return(context.Response.WriteAsync(JsonConvert.SerializeObject(content)));
                },
                OnTokenValidated = context =>
                {
                    var claim = context.Principal.Claims.SingleOrDefault(x => x.Type == "id");
                    if (claim is null)
                    {
                        return(Task.CompletedTask);
                    }
                    var id    = Convert.ToInt32(claim.Value);
                    var token = (context.SecurityToken as JwtSecurityToken).RawData;

                    var tokens = TokenDictionary.GetTokens(id);
                    if (tokens is null)
                    {
                        return(Task.CompletedTask);
                    }

                    if (!tokens.Any(x => x.Value.Equals(token, StringComparison.OrdinalIgnoreCase)))
                    {
                        context.Fail("Token invalid");
                    }

                    return(Task.CompletedTask);
                }
            };
            return(options);
        }
    }
Exemple #2
0
        /// <summary>
        /// Constructs a document model
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="name">The name.</param>
        /// <param name="context">The context.</param>
        /// <param name="stemmContext">The stemm context.</param>
        /// <param name="tokenizer">The tokenizer.</param>
        /// <param name="metrics">The metrics.</param>
        /// <returns></returns>
        public SpaceDocumentModel ConstructDocument(string text, String name, SpaceModel context, StemmingContext stemmContext, ITokenizer tokenizer, Boolean isKnownDocument, ContentMetrics metrics = null)
        {
            var tokens = tokenizer.Tokenize(text);

            if (metrics != null)
            {
                metrics.TokensDoc += tokens.Length;                                // <----- token length
            }
            TokenDictionary tokenDictionary = new TokenDictionary(tokens);


            if (metrics != null)
            {
                metrics.UniqueTokensDoc += tokenDictionary.Count;                  // <---- unique tokens
            }
            TokenDictionary stemmDictionary = new TokenDictionary();

            List <String> tkn = tokenDictionary.GetTokens();

            for (int i2 = 0; i2 < tkn.Count; i2++)
            {
                String stk = stemmContext.Stem(tkn[i2]);
                stemmDictionary.CountToken(stk, tokenDictionary.GetTokenFrequency(tkn[i2]));
            }

            //  context.terms.MergeDictionary(stemmDictionary);

            if (metrics != null)
            {
                metrics.StemmedTokensDoc += stemmDictionary.Count;                  // <---- stemmed
            }
            SpaceDocumentModel document = new SpaceDocumentModel();

            document.name   = name;
            document.terms  = stemmDictionary;
            document.Length = tokens.Length;

            if (spaceSettings.DoMaintainWordIndex)
            {
                document.Words = new int[document.Length];
            }

            Int32 c = 0;

            for (int i = 0; i < tokens.Length; i++)
            {
                String stk = stemmContext.Stem(tokens[i]);

                if (isKnownDocument)
                {
                    context.terms_known_label.AddToken(stk);
                }
                else
                {
                    context.terms_unknown_label.AddToken(stk);
                }

                if (spaceSettings.DoMaintainWordIndex)
                {
                    document.Words[c] = context.terms.GetTokenID(stk);
                }
                c++;
            }

            document.name = name;

            // context.documents.Add(document);



            return(document);
        }