UWInfo Blog
發表新文章
[Join] | [忘記密碼] | [Login]
搜尋

搜尋意見
文章分類-Bike
[所有文章分類]
  • ASP.NET (10)
  • ASP.NET2.0 (6)
  • ASP.NET4.0 (7)
  • JavaScript (11)
  • jQuery (9)
  • FireFox (0)
  • UW系統設定 (0)
  • SQL (10)
  • SQL 2008 (7)
  • mirror (0)
  • SVN (2)
  • IE (2)
  • IIS (9)
  • IIS6 (0)
  • 閒聊 (0)
  • W3C (0)
  • 作業系統 (1)
  • C# (0)
  • CSS (3)
  • FileServer (0)
  • HTML 5 (3)
  • CKEditor (2)
  • UW.dll (3)
  • Visual Studio (4)
  • Browser (2)
  • SEO (1)
  • Google Apps (0)
  • 網站輔助系統 (0)
  • DNS (0)
  • SMTP (1)
  • 網管 (1)
  • 社群API (0)
  • SSL (0)
  • App_Inventor (0)
  • URLRewrite (0)
  • 開發工具 (0)
  • JSON (0)
  • Excel2007 (0)
  • 試題 (3)
  • LINQ (0)
  • bootstrap (0)
  • Vue (0)
  • IIS7 (0)
  • foodpanda (0)
  • 編碼 (0)
  • 資安 (0)
  • Sourcetree (0)
  • MAUI (1)
  • CMD (0)
  • my sql (0)
所有文章分類
[Bike的分類]
  • ASP.NET (48)
  • ASP.NET2.0 (15)
  • ASP.NET4.0 (34)
  • JavaScript (49)
  • jQuery (26)
  • FireFox (4)
  • UW系統設定 (3)
  • SQL (39)
  • SQL 2008 (25)
  • mirror (4)
  • SVN (4)
  • IE (9)
  • IIS (20)
  • IIS6 (1)
  • 閒聊 (7)
  • W3C (6)
  • 作業系統 (9)
  • C# (24)
  • CSS (12)
  • FileServer (1)
  • HTML 5 (11)
  • CKEditor (3)
  • UW.dll (13)
  • Visual Studio (16)
  • Browser (8)
  • SEO (1)
  • Google Apps (3)
  • 網站輔助系統 (4)
  • DNS (5)
  • SMTP (4)
  • 網管 (11)
  • 社群API (3)
  • SSL (4)
  • App_Inventor (1)
  • URLRewrite (2)
  • 開發工具 (6)
  • JSON (1)
  • Excel2007 (1)
  • 試題 (3)
  • LINQ (1)
  • bootstrap (0)
  • Vue (3)
  • IIS7 (3)
  • foodpanda (2)
  • 編碼 (2)
  • 資安 (3)
  • Sourcetree (1)
  • MAUI (1)
  • CMD (1)
  • my sql (1)
最新回應
  • Newtonsoft.Json.JsonConvert.DeserializeObject 失敗的情況
    test...more
  • 用 Javascript 跨網頁讀取 cookie (Cookie cross page, path of cookie)
    ...more
  • IIS 配合 AD (Active Directory) 認証, 使用 .Net 6.0
    太感謝你了~~~你救了我被windows 認證卡了好幾天QQ...more
  • PostgreSQL 的 monitor trigger
    FOR EACH ROW 可能要改為 FOR EACH STATEMENT ...more
  • 用 Javascript 跨網頁讀取 cookie (Cookie cross page, path of cookie)
    ;;;;...more
  • 匯出 let's encryption的憑証
    1. win-acme 選擇 A: Manage renewals找到你要轉移的憑證名稱和密碼2. ...more
  • 資安問題 -- 存入 DB 後顯示內容
    伺服器隱碼問題1 寫入DB前不是當成字串就執行某些事情 (某些server 漏洞? 許功蓋?)2 寫...more
  • 匯出 let's encryption的憑証
    【注意】要打開 win-acme <<這個軟體匯出憑證...more
  • Swagger 的問題解決 (NSwag)
    DefaultController所產出的文建會讓Swagger讀不懂報錯[ApiExplorerS...more
  • 合併兩個 Expression -- Combining two expressions (Expression>)
    官方文件就有寫啊,這麼多方法不全部看個一遍說不過去吧~https://learn.microsoft...more
標籤
  • sql
  • node
  • -1873
  • CROS
  • lock
  • a
  • at9jiYNK
  • server job
  • 2625
  • [t]
  • tls
  • 390
  • checked
  • 8 UNION AL
  • Image
  • 指令
  • admin
  • 驗證碼
  • post
  • entity
  • asp
  • cheap
  • admin[t]
  • FB3
  • request.qu
  • 100[t]
  • tim
  • encrypt
  • ctrl
  • sonarcube
  • sp_
  • 3662
  • write
  • deferred
  • GN.LinePay
  • 9910
  • 122
  • ad
  • IIS
  • 9367-9367
  • 背景
  • 18
  • -2947
  • 70
  • [t],
  • 驗證
  • 安裝
  • shop ORDER
  • Config ORD
  • 662
頁數 4 / 26 上一頁 下一頁
資安問題 -- Session Id 為什麼要加密後再存於 Client 端
接到弱掃報告指出以下的程式碼會造成危害:

string Url = WebConfigurationManager.AppSettings["SkyShopCheckoutURL"] + $"/api/token/ {Su.Fsc.SessionId}"; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpClient client = Su.Wu.GetClient();
var response = await client.GetAsync(Url);
var message = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject(message);

沒想到吧...
More...
Bike, 2022/9/23 上午 05:40:51
資安問題 -- 使用者輸入的資料成為檔名的一部份
使用者輸入的資料包括 cookie 的資料, header 的資料,form 和 query string 的資料。

可以使用以下的 function 避免寫入錯誤的檔案:

var newFilename = filename.Replace("..", "").ValidFilenameCharacters();
More...
Bike, 2022/9/22 下午 09:40:56
資安問題 -- Random 的替代 function
        static System.Security.Cryptography.RandomNumberGenerator RandomNumberGenerator = null;

        /// <summary>
        /// 回傳大於等於 0 小於 1
        /// </summary>
        /// <returns></returns>
        public static double NextDouble()
        {
            if (RandomNumberGenerator == null || LastRandomGenerateTime < DateTime.Now.AddMinutes(-1))
            {
                RandomNumberGenerator = System.Security.Cryptography.RandomNumberGenerator.Create();
            }

            // Generate four random bytes
            byte[] four_bytes = new byte[4];
            RandomNumberGenerator.GetBytes(four_bytes);

            // Convert the bytes to a UInt32
            uint scale = BitConverter.ToUInt32(four_bytes, 0);

            // And use that to pick a random number >= min and < max
            // 0 <= (scale / (uint.MaxValue + 1.0)) < 1
            return scale / (uint.MaxValue + 1.0);
        }

        /// <summary>
        /// 回傳值介於 min ~ (max - 1)
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static int GetRandomInt(int min, int max)
        {
            if(max < min)
            {
                throw new Exception("最大值不可小於最小值");
            }

            // Generate four random bytes
            byte[] four_bytes = new byte[4];
            RandomNumberGenerator.GetBytes(four_bytes);

            // And use that to pick a random number >= min and < max
            return (int)(min + (max - min) * NextDouble()); //因為 NextDouble 會小於 1,所以用無條件捨去.
        }

        /// <summary>
        /// 產生 0 ~ (max - 1) 的亂數
        /// </summary>
        /// <param name="max"></param>
        /// <returns></returns>
        public static int GetRandomInt(int max)
        {
            return GetRandomInt(0, max);
        }

        /// <summary>
        /// 產生 0 ~ (int.MaxValue - 1) 的亂數
        /// </summary>
        /// <returns></returns>
        public static int GetRandomInt()
        {
            return GetRandomInt(int.MaxValue);
        }
More...
Bike, 2022/9/22 下午 08:30:03
資安問題 -- 存入 DB 後顯示內容
把使用者輸入的內容顯示於網頁上,需要注意什麼 ?

客人在前台例如輸入留言,客服在後台查看留言。
More...
Bike, 2022/9/22 下午 03:09:40
資安問題 -- ServerVariables
var ip =  HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

return Content("發生錯誤, 請連絡系統管理員. 並告知您的 IP 是: " + ip + ".");
More...
Bike, 2022/9/22 下午 03:07:01
VS2022 上面安裝 NSwag (.Net 6.0)
改用 NSwag 時直接 Copy 舊專案,發現找不到 AddOpenApiDocument 和 UseOpenApi 以及 UseSwaggerUi3 如下圖:

 
 

找了一些文件,要求安裝套件,都沒有用,後來發現在 Dependencies 的 Packages 直接新增 NSwag.AspNetCore 即可 





 
不要用 NuGet Package Manager 哦...
More...
Bike, 2022/9/3 下午 05:31:14
Postgres 中使用 Transaction (TransactionScope) 必需要自已實做重試的功能
在 Postgres 使用 TransactionScope 似乎很空易就會發生 Transaction Abort 的情況。所以必需自行實做 retry 的機制

            int tries = 0;
            while (tries < Su.PgSql.maxTransactionAborted)
            {
                try
                {
                    using (var scop = new System.Transactions.TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                    {
                        var context = ShopBandContext.Context;

                        updateCount = await context.UpdateAsync<MarketDiscount>(dto, Sc.ModifyInfo,
                            onlyColumns: "StartAt,EndAt,NewPrice,FrontEndMemo,BackEndMemo");

                        scop.Complete();
                        break; //這個 Break 很重要
                    }
                }
                catch (Exception ex)
                {
                    // 我發現因為是非同步模式,所以要檢查很多層的 InnerException
                    if (tries < Su.PgSql.maxTransactionAborted
                        && ((ex.InnerException != null && ex.InnerException is Npgsql.PostgresException && ((Npgsql.PostgresException)ex.InnerException).SqlState == "40001")
                            || (ex.InnerException.InnerException != null && ex.InnerException.InnerException is Npgsql.PostgresException && ((Npgsql.PostgresException)ex.InnerException.InnerException).SqlState == "40001")))
                    {
                        //隨機休息 10 ~ 20 ms, 等另一個 job 完工
                        System.Threading.Thread.Sleep(Su.MathUtil.Random.Next(10, 20));
                        tries++;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
More...
Bike, 2022/8/25 上午 09:19:35
The JSON value could not be converted to System.Nullable`1[System.DateTime]
.net 6 在 model 中上傳日期字串時,如果遇到 "The JSON value could not be converted to System.Nullable`1[System.DateTime]"  這個錯誤,解決方法如下:

1. 安裝套件: Microsoft.AspNetCore.Mvc.NewtonsoftJson

2. 在 program.cs 中,原來來的

builder.Services.AddControllers()

改為 (其 options 的部份非必要)

builder.Services.AddControllers()
    .AddNewtonsoftJson(options =>
     {
         options.SerializerSettings.ContractResolver = new Su.CamelCaseContractResolver();
     });
More...
Bike, 2022/8/21 下午 10:12:43
合併兩個 Expression -- Combining two expressions (Expression>)
找了很久,原來就在 梨子給的範例裡。

假設有兩個 expression: e1, e2

            var combineBody = Expression.AndAlso(e1.Body, Expression.Invoke(e2, e1.Parameters[0]));
            var finalExpression = Expression.Lambda<Func<TestClass, bool>>(combineBody, e1.Parameters).Compile();


同理,把上面的 AndAlso 換成 OrElse 就可以用 Or 合併。

即使只有兩行,還是不太可能背起來,所以當然要來做一下擴充

    public static class ExpressionExtension
    {
        public static Expression<Func<T, bool>> AndAlso<T>(this Expression<Func<T, bool>> e1, Expression<Func<T, bool>> e2)
        {
            var combineE = Expression.AndAlso(e1.Body, Expression.Invoke(e2, e1.Parameters[0]));

            return Expression.Lambda<Func<T, bool>>(combineE, e1.Parameters);
        }

        public static Expression<Func<T, bool>> OrElse<T>(this Expression<Func<T, bool>> e1, Expression<Func<T, bool>> e2)
        {
            var combineE = Expression.OrElse(e1.Body, Expression.Invoke(e2, e1.Parameters[0]));

            return Expression.Lambda<Func<T, bool>>(combineE, e1.Parameters);
        }
    }


使用範例:

            Expression<Func<Market, bool>> e1 = e => e.Is_Deleted == isDelete;

            Expression<Func<Market, bool>> e2 = e => string.IsNullOrEmpty(marketNo) || e.MarketNo.ToUpper().Contains(marketNo.ToUpper());

            return new
            {
                andMarkets = Ds.PageContext.ShopBandContext.Markets.Where(e1.AndAlso(e2)).ToList(),
                orMarkets = Ds.PageContext.ShopBandContext.Markets.Where(e1.OrElse(e2)).ToList(),
            };



 再補兩個擴充, 可以把多個 Expression 用 AndAlso 或 OrElse 串在一起:

        public static Expression<Func<T, bool>> OrElseAll<T>(this IEnumerable<Expression<Func<T, bool>>> exps)
        {
            if (exps.Count() == 1)
            {
                return exps.First();
            }

            var e0 = exps.First();

            var orExp = exps.Skip(1).Aggregate(e0.Body, (x, y) => Expression.OrElse(x, Expression.Invoke(y, e0.Parameters[0])));

            return Expression.Lambda<Func<T, bool>>(orExp, e0.Parameters);
        }

        public static Expression<Func<T, bool>> AndAlsoAll<T>(this IEnumerable<Expression<Func<T, bool>>> exps)
        {
            if (exps.Count() == 1)
            {
                return exps.First();
            }

            var e0 = exps.First();

            var orExp = exps.Skip(1).Aggregate(e0.Body, (x, y) => Expression.AndAlso(x, Expression.Invoke(y, e0.Parameters[0])));

            return Expression.Lambda<Func<T, bool>>(orExp, e0.Parameters);
        }


使用範例:

            Expression<Func<Market, bool>> q = e =>
            e.Is_Deleted == "N"
            && (string.IsNullOrEmpty(marketNo) || e.MarketNo.ToLower().Contains(marketNo.ToLower()))
            && (string.IsNullOrEmpty(isCombination) || isCombination != "Y" || e.TypeEnum == 200);

            if (!string.IsNullOrEmpty(name))
            {
                var nameList = name.Split(',').Select(e => e.Trim())
                    .Where(e => !string.IsNullOrEmpty(e));

                if (nameList.Any())
                {
                    q = q.AndAlso(nameList
                        .Select(s => (Expression<Func<Market, bool>>)(e => e.Name.ToLower().Contains(s.ToLower())))
                        .OrElseAll());
                }
            }






另外在 google,整理了 stackoverflow 幾篇文章之後得到的另一個方法,比較複雜, 不過可以讓人理解一下 Expression 比較底層的東西,也留下來參考一下。

    internal class MergeTool : ExpressionVisitor
    {
        private readonly ParameterExpression _parameter;

        protected override Expression VisitParameter(ParameterExpression node)
        {
            return base.VisitParameter(_parameter);
        }

        internal MergeTool(ParameterExpression parameter)
        {
            _parameter = parameter;
        }

        public static Expression<Func<T, bool>> MergedExpression<T>(Expression<Func<T, bool>> e1, Expression<Func<T, bool>> e2)
        {
            ParameterExpression param = Expression.Parameter(typeof(T));

            BinaryExpression MergeBody = Expression.AndAlso(e1.Body, e2.Body);

            var ReplacedBody = (BinaryExpression)new MergeTool(param).Visit(MergeBody);

            return Expression.Lambda<Func<T, bool>>(ReplacedBody, param);
        }
    }

使用時要 Compile

            var mergedExpression = MergeTool.MergedExpression(e1, e2);

            var list = testList.Where(mergedExpression.Compile());

More...
Bike, 2022/8/13 下午 05:53:28
關於 PostgreSQL
-- 自動更新某個欄位

CREATE OR REPLACE FUNCTION before_market_update ()
RETURNS TRIGGER AS 
$$
BEGIN
    NEW.LMDT = LocalTimeStamp;
    RETURN NEW;
END;
$$
LANGUAGE 'plpgsql';

CREATE TRIGGER updt_market
  BEFORE UPDATE
  ON "Market"
  FOR EACH ROW
  EXECUTE PROCEDURE before_market_update();
More...
Bike, 2022/8/12 下午 09:25:57
|< 12345678910… >|
頁數 4 / 26 上一頁 下一頁
~ Uwinfo ~