Exemplo n.º 1
0
        virtual public List <IOptionsProvider> Load(HttpContext ctx, IOptionsDictionary dict)
        {
            var emptyPrefix = String.IsNullOrEmpty(SourcePrefix);
            var cookie      = ctx.Request.Cookies[CookieName];
            var res         = new List <IOptionsProvider>();

            if (string.IsNullOrEmpty(cookie))
            {
                return(res);
            }
            cookie = WebUtility.UrlDecode(cookie);
            var toAdd = (JsonConvert.DeserializeObject(cookie, typeof(List <KeyValuePair <string, string> >)) as List <KeyValuePair <string, string> >)
                        .Where(m => emptyPrefix ||
                               m.Key == SourcePrefix ||
                               (m.Key.StartsWith(SourcePrefix) && m.Key[SourcePrefix.Length] == '.'))
                        .Select(m => new
            {
                Key   = Prefix + "." + (emptyPrefix ? m.Key : m.Key.Substring(SourcePrefix.Length + 1)),
                Value = m.Value
            });

            foreach (var x in toAdd)
            {
                var pres = dict.AddOption(this, x.Key, x.Value, Priority);
                if (pres != null)
                {
                    res.Add(pres);
                }
            }
            return(res);
        }
        virtual public void Save(HttpContext ctx, IOptionsDictionary dict)
        {
            var emptyPrefix = String.IsNullOrEmpty(SourcePrefix);
            var toAdd       = ctx.User.Claims.Where(m => m.Type == SourcePrefix || (m.Type.StartsWith(SourcePrefix) && m.Type[SourcePrefix.Length] == '/'))
                              .Select(m => new
            {
                Key   = m.Type,
                Value = m
            });

            var comparer = new Dictionary <string, System.Security.Claims.Claim>();

            foreach (var x in toAdd)
            {
                comparer.Add(x.Key, x.Value);
            }

            var changes = dict.GetEntries(Prefix);
            List <System.Security.Claims.Claim> ToAddC   = new List <System.Security.Claims.Claim>();
            List <System.Security.Claims.Claim> ToRemove = new List <System.Security.Claims.Claim>();

            foreach (var c in changes)
            {
                var key = emptyPrefix ? c.Key.Replace(".", "/") : SourcePrefix + "/" + c.Key.Replace(".", "/");
                var val = c.Value;
                System.Security.Claims.Claim prev = null;
                if (comparer.TryGetValue(key, out prev))
                {
                    ToAddC.Add(new System.Security.Claims.Claim(prev.Type, val, prev.ValueType, prev.Issuer, prev.OriginalIssuer, prev.Subject));
                    ToRemove.Add(prev);
                }
                else
                {
                    ToAddC.Add(new System.Security.Claims.Claim(key, val));
                }
            }
            if (changes.Count > 0)
            {
                UserManager <T>   um = ctx.RequestServices.GetService(typeof(UserManager <T>)) as UserManager <T>;
                SignInManager <T> sm = ctx.RequestServices.GetService(typeof(SignInManager <T>)) as SignInManager <T>;
                var aUserT           = um.FindByNameAsync(ctx.User.Identity.Name);
                aUserT.Wait();
                if (ToRemove.Count > 0)
                {
                    var t = um.RemoveClaimsAsync(aUserT.Result, ToRemove);
                    t.Wait();
                }
                if (ToAddC.Count > 0)
                {
                    var t = um.AddClaimsAsync(aUserT.Result, ToAddC);
                    t.Wait();
                }
                var to = sm.SignOutAsync();
                to.Wait();
                var tl = sm.SignInAsync(aUserT.Result, PersistentSignIn, AuthenticationMethod);
                tl.Wait();
            }
        }
Exemplo n.º 3
0
        virtual public void Save(HttpContext ctx, IOptionsDictionary dict)
        {
            var serialized = WebUtility.UrlEncode(JsonConvert.SerializeObject(dict.GetEntries(Prefix, String.IsNullOrEmpty(SourcePrefix) ? null : SourcePrefix)));

            ctx.Response.Cookies.Append(CookieName, serialized, new CookieOptions
            {
                HttpOnly = false,
                Expires  = DateTime.Now.Add(Duration)
            });
        }
        public T Bind <T>(IOptionsDictionary dict, ProvidersDictionary prov, HttpContext ctx)
            where T : class, new()
        {
            string prefix = null;

            if (this.TryGetValue(typeof(T), out prefix))
            {
                prov.AddToRequest(prefix, ctx, dict);
                return((T)dict.GetOptionObject(prefix, typeof(T), new T()));
            }
            return(null);
        }
 private void loadSection(IOptionsDictionary dict, string prefix, IConfigurationSection section, List <IOptionsProvider> res)
 {
     if (section.Value != null)
     {
         var pres = dict.AddOption(this, prefix, section.Value, Priority);
         if (pres != null)
         {
             res.Add(pres);
         }
     }
     foreach (var child in section.GetChildren())
     {
         loadSection(dict, prefix + "." + child.Key, child, res);
     }
 }
Exemplo n.º 6
0
        virtual public List <IOptionsProvider> Load(HttpContext ctx, IOptionsDictionary dict)
        {
            var             res    = new List <IOptionsProvider>();
            UserManager <T> um     = ctx.RequestServices.GetService(typeof(UserManager <T>)) as UserManager <T>;
            var             aUserT = um.FindByNameAsync(ctx.User.Identity.Name);

            aUserT.Wait();
            var user    = aUserT.Result;
            var options = UserOptions(user);

            if (options == null)
            {
                return(res);
            }
            return(dict.AddOptionObject(this, Prefix, options, Priority, 1));
        }
        public void Save <T> (T options, IOptionsDictionary dict, HttpContext ctx)
        {
            string prefix = null;
            HashSet <IOptionsProvider> set = null;

            if (this.TryGetValue(typeof(T), out prefix))
            {
                set = new HashSet <IOptionsProvider>(dict.AddOptionObject(null, prefix, uint.MaxValue));
                foreach (var x in set)
                {
                    if (x.Enabled(ctx) && x.CanSave)
                    {
                        x.Save(ctx, dict);
                    }
                }
            }
        }
        virtual public List <IOptionsProvider> Load(HttpContext ctx, IOptionsDictionary dict)
        {
            var emptyPrefix = String.IsNullOrEmpty(SourcePrefix);
            var res         = new List <IOptionsProvider>();

            if (UseForm && ctx.Request.HasFormContentType)
            {
                var form = ctx.Request.Form;

                var toAdd = form.Where(m => emptyPrefix ||
                                       (m.Key.StartsWith(SourcePrefix) && m.Key.Length > SourcePrefix.Length + 1 && m.Key[SourcePrefix.Length] == '.'))
                            .Select(m => new
                {
                    Key   = Prefix + "." + (emptyPrefix ? m.Key : m.Key.Substring(SourcePrefix.Length + 1)),
                    Value = m.Value
                });
                foreach (var x in toAdd)
                {
                    var pres = dict.AddOption(this, x.Key, x.Value, Priority);
                    if (pres != null)
                    {
                        res.Add(pres);
                    }
                }
            }
            if (UseParams)
            {
                var pars  = ctx.Request.Query;
                var toAdd = pars.Where(m => emptyPrefix ||
                                       (m.Key.StartsWith(SourcePrefix) && m.Key.Length > SourcePrefix.Length + 1 && m.Key[SourcePrefix.Length] == '.'))
                            .Select(m => new
                {
                    Key   = Prefix + "." + (emptyPrefix ? m.Key : m.Key.Substring(SourcePrefix.Length + 1)),
                    Value = m.Value
                });
                foreach (var x in toAdd)
                {
                    var pres = dict.AddOption(this, x.Key, x.Value, Priority);
                    if (pres != null)
                    {
                        res.Add(pres);
                    }
                }
            }
            return(res);
        }
        virtual public List <IOptionsProvider> Load(HttpContext ctx, IOptionsDictionary dict)
        {
            if (string.IsNullOrWhiteSpace(SourcePrefix))
            {
                SourcePrefix = Prefix;
            }
            var                     path = SourcePrefix.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            IConfiguration          currConfiguration = Configuration;
            List <IOptionsProvider> res = new List <IOptionsProvider>();

            for (int i = 0; i < path.Length; i++)
            {
                currConfiguration = currConfiguration.GetChildren().Where(m => m.Key == path[i]).FirstOrDefault();
                if (currConfiguration == null)
                {
                    return(res);
                }
            }
            loadSection(dict, Prefix, currConfiguration as ConfigurationSection, res);
            return(res);
        }
        virtual public List <IOptionsProvider> Load(HttpContext ctx, IOptionsDictionary dict)
        {
            var res         = new List <IOptionsProvider>();
            var emptyPrefix = String.IsNullOrEmpty(SourcePrefix);
            var toAdd       = ctx.User.Claims.Where(m => m.Type == SourcePrefix || (m.Type.StartsWith(SourcePrefix) && m.Type[SourcePrefix.Length] == '/'))
                              .Select(m => new
            {
                Key   = emptyPrefix ? Prefix + "." + m.Type.Replace("/", ".") : Prefix + "." + m.Type.Substring(SourcePrefix.Length + 1).Replace("/", "."),
                Value = m.Value
            });

            foreach (var x in toAdd)
            {
                var pres = dict.AddOption(this, x.Key, x.Value, Priority);
                if (pres != null)
                {
                    res.Add(pres);
                }
            }
            return(res);
        }
        public void AddToRequest(string prefix, HttpContext context, IOptionsDictionary dict)
        {
            uint maxPriority = 0;
            List <IOptionsProvider>    addedProviders = new List <IOptionsProvider>();
            HashSet <IOptionsProvider> set            = new HashSet <IOptionsProvider>();

            foreach (var x in allProviders)
            {
                if (x.Key == prefix || (prefix.StartsWith(x.Key) && prefix[x.Key.Length] == '.'))
                {
                    foreach (var y in x.Value)
                    {
                        if (y.Enabled(context) && !requestProviders.Contains(y))
                        {
                            if (y.Priority > maxPriority)
                            {
                                maxPriority = y.Priority;
                            }
                            addedProviders.Add(y);
                            set.UnionWith(y.Load(context, dict));
                            requestProviders.Add(y);
                        }
                    }
                }
            }
            foreach (var x in addedProviders)
            {
                if (x.AutoCreate && x.CanSave && x.Priority < maxPriority)
                {
                    set.Add(x);
                }
            }
            foreach (var x in set)
            {
                if (x.Enabled(context) && x.CanSave && (x.AutoSave || x.AutoCreate))
                {
                    x.Save(context, dict);
                }
            }
        }
        virtual public List <IOptionsProvider> Load(HttpContext ctx, IOptionsDictionary dict)
        {
            var emptyPrefix = String.IsNullOrEmpty(SourcePrefix);

            string value = null;

            if (ctx.Request.HasFormContentType)
            {
                value = ctx.Request.Form[FieldName];
            }
            if (value == null)
            {
                value = ctx.Request.Query[FieldName];
            }
            var res = new List <IOptionsProvider>();

            if (string.IsNullOrEmpty(value))
            {
                return(res);
            }
            var toAdd = (JsonConvert.DeserializeObject(value, typeof(List <KeyValuePair <string, string> >)) as List <KeyValuePair <string, string> >)
                        .Where(m => emptyPrefix ||
                               m.Key == SourcePrefix ||
                               (m.Key.StartsWith(SourcePrefix) && m.Key[SourcePrefix.Length] == '.'))
                        .Select(m => new
            {
                Key   = Prefix + "." + (emptyPrefix ? m.Key : m.Key.Substring(SourcePrefix.Length + 1)),
                Value = m.Value
            });

            foreach (var x in toAdd)
            {
                var pres = dict.AddOption(this, x.Key, x.Value, Priority);
                if (pres != null)
                {
                    res.Add(pres);
                }
            }
            return(res);
        }
Exemplo n.º 13
0
        virtual public void Save(HttpContext ctx, IOptionsDictionary dict)
        {
            var             res    = new List <IOptionsProvider>();
            UserManager <T> um     = ctx.RequestServices.GetService(typeof(UserManager <T>)) as UserManager <T>;
            var             aUserT = um.FindByNameAsync(ctx.User.Identity.Name);

            aUserT.Wait();
            var user = aUserT.Result;

            if (ApplyOptionsToUser == null)
            {
                var options = UserOptions(user);
                if (options == null)
                {
                    options = new M();
                }
                typeof(T).GetProperty(OptionFieldName).SetValue(user, options);
                dict.GetOptionObject(Prefix, typeof(M), options);
            }
            else
            {
                var options = dict.GetOptionObject(Prefix, typeof(M)) as M;
                ApplyOptionsToUser(user, options);
            }

            if (SaveDbContext || RelogUserAfterSave)
            {
                var aw = um.UpdateAsync(user);
                aw.Wait();
                if (RelogUserAfterSave)
                {
                    SignInManager <T> sm = ctx.RequestServices.GetService(typeof(SignInManager <T>)) as SignInManager <T>;
                    var to = sm.SignOutAsync();
                    to.Wait();
                    var tl = sm.SignInAsync(aUserT.Result, PersistentSignIn, AuthenticationMethod);
                    tl.Wait();
                }
            }
        }
Exemplo n.º 14
0
        virtual public List <IOptionsProvider> Load(HttpContext ctx, IOptionsDictionary dict)
        {
            var          res = new List <IOptionsProvider>();
            StringValues value;

            if (UseForm && ctx.Request.HasFormContentType)
            {
                var form = ctx.Request.Form;

                foreach (var x in clauses)
                {
                    if (form.TryGetValue(x, out value))
                    {
                        var pres = dict.AddOption(this, Prefix + "." + x, value.ToString(), Priority + 1);
                        if (pres != null)
                        {
                            res.Add(pres);
                        }
                    }
                }
            }
            if (UseParams)
            {
                var pars = ctx.Request.Query;

                foreach (var x in clauses)
                {
                    if (pars.TryGetValue(x, out value))
                    {
                        var pres = dict.AddOption(this, Prefix + "." + x, value.ToString(), Priority);
                        if (pres != null)
                        {
                            res.Add(pres);
                        }
                    }
                }
            }
            return(res);
        }
 virtual public void Save(HttpContext ctx, IOptionsDictionary dict)
 {
     throw new NotImplementedException();
 }
 public DefaultPreferencesService(IOptionsDictionary optionsDictionary)
 {
     this.optionsDictionary = optionsDictionary;
 }