2018年11月13日 星期二

[ASP.Net MVC] Github OAuth外部驗證無法使用(要求已經中止: 無法建立 SSL/TLS 的安全通道/loginInfo為null)

前言:
OAuth是一種外部驗證的標準,可讓第三方應用程式,存取資源擁有者的資源。
通過驗證後,第三方應用程式會從OAuth Server拿到token,
即可使用token存取資源伺服器上的資源了。

.Net MVC 5預設有提供外部驗證(Google、FB、Microsoft與Twitter等),
如要使用Github OAuth驗證,NuGet上也有package可直接使用:
裝好package後,打開Startup.Auth.cs,
輸入app.UseGitHubAuthentication("clientID", "clientSecret");
基本上就可使用了。

需啟用https,才可以進行OAuth驗證。

不過在本機使用.Net framework 4.5.2+MVC 5.2.3驗證卻無法登入:
按下外部登入的GitHub按鈕,狀態列顯示瀏覽器在動作,但過了一會,
發現畫面依然停留在登入畫面,WTF??

取得Owin.Security.Providers.GitHub的source code來trace,
發現在發request要token這裡,
出現Exception:「要求已經中止: 無法建立 SSL/TLS 的安全通道」,
(The request was aborted: Could not create SSL/TLS secure channel.)
導致ExternalLoginCallback中loginInfo為null,又回到Login頁面。

那為何出現該Exceptioin? 參考黑大與其他文章,
原來是.Net 4.5預設https的protocol是TLS 1.0,但TLS 1.0因安全性問題,
已被大家捨棄,使用TLS 1.2以上是現行的做法。

解決辦法:
● 因.Net 4.6預設使用TLS 1.2,新專案可直接使用framework 4.6以上,試過ok,直接升級framework試過不行。
● 若是參考source code,可以在handler要token前的request加入:
 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolTypeExtensions.Tls12;

● 修改registry機碼,將環境改為TLS 1.2,可參考下圖。



參考資料:
https://tools.ietf.org/html/rfc6749#section-1.3.1
https://blog.darkthread.net/blog/disable-tls-1-0-issues
https://support.microsoft.com/en-us/help/3154520/support-for-tls-system-default-versions-included-in-the-net-framework






2018年8月30日 星期四

[Python Package] matplotlib使用記錄

前言:
資料視覺化是了解資料與報告成果的重要方式,在Python常用matplotlib套件來畫圖,
以下紀錄使用的語法。


語法:

在jupyter notebook常用,宣告後,不用每次寫plt.show()。

matplotlib可自訂繪圖風格,ggplot風格就是模仿R的知名繪圖套件ggplot2,讓繪圖更美觀。

一般二維的折線圖,使用plot函式,簽章如下:

[]代表optional,fmt表format字串,可用來決定折線顏色、資料點標記與折線樣式。

例:'yo-'表line為黃色、資料點圓形、line粗體。


plot函式使用,例:
plt.plot([0,1,2], [0,10,0])
plt.plot([0,1,2], [0,10,0], 'g.-')


to be continued...


參考資料:
https://matplotlib.org/gallery/style_sheets/ggplot.html#sphx-glr-download-gallery-style-sheets-ggplot-py
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html


2018年8月22日 星期三

[.Net MVC] Items could not be selected(null) in Multiple Select(ListBox)

前言:
網頁中常用Multi Select或稱ListBox供使用者多選Items,如:
<select multiple>
<option value="Taiwan">Taiwan</option>
<option value="USA">USA</option>
</select>
 或
@Html.ListBox("country", null, new {multiple = multiple})

當option items render後,又沒有被點選,會發生回傳item為null的狀況,
該如何處理?


作法:
● 在form post前,將option加上selected的屬性。
$('form').submit(function(){
   $('#target option').attr("selected", "selected");
});
若上述做法行不通,可使用下面方法:
● append option value到某一hidden element,再post到後端。



參考資料:
  

2018年6月26日 星期二

[Python/Package] macOS安裝keras與TensorFlow(搭配Virtualenv與jupyter notebook)

前言:
Keras與TensorFlow都是machine learning的套件,
Keras較適合入門,並可選擇run在TensorFlow、CNTK或Theano上。
該例使用Python 3,另TensorFlow website推薦macOS在10.12.6(Sierra)或以上。

安裝步驟:
打開Terminal,打指令:
virtualenv --system-site-packages -p python3 targetDirectory

~/keras代表在使用者下建立keras資料夾。

接著打指令:
cd targetDirectory

再來打指令:
$ source ./bin/activate # If using bash, sh, ksh, or zsh

繼續打指令:
(targetDirectory)$ pip3 install --upgrade keras # for Python 3.n
(targetDirectory)$ pip3 install --upgrade tensorflow # for Python 3.n


此時已將keras跟TensorFlow安裝在Virtualenv內了。
安裝後,可以做個簡單的測試,是否有成功安裝:
(targetDirectory)$ python
(targetDirectory)$ import tensorflow as tf



執行import後,沒有錯誤訊息,代表有安裝進去。
最後在Virtualenv安裝jupyter notebook的kernel:
(targetDirectory)$ pip3 install ipykernel
(targetDirectory)$ python -m ipykernel install --user --name=virtualenv-name


最後,啟動jupyter notebook:
(targetDirectory)$ jupyter notebook
點選jupyter notebook主畫面右上角的New,選擇剛建立在Virtualenv的kernel即可:



最後,執行畫面如下 :


參考資料:
https://www.tensorflow.org/install/install_mac
https://stackoverflow.com/questions/42449814/running-jupyter-notebook-in-a-virtualenv-installed-sklearn-module-not-available

2018年5月31日 星期四

[CSS] 如何讓div element內的單行文字垂直置中(vertical center)

說明:
網頁上常用div element顯示區塊文字,水平置中可直接指定text-align:center,例:
<div style="text-align:center">test</div>
但文字垂直置中並沒有屬性可直接設定,
雖然有個vertical-align的屬性,不過是用在圖片置中上的...
那文字垂直置中該怎麼做?


作法:
答案是height跟line-height設一樣即可,例:
<div style="height:2vh;line-height:2vh">test</div>

不過為何這樣設定就可以讓文字垂直置中??
一般而言,line-height會大於font-size,
Content Area的高度由font-size決定,
 line-height扣掉font-size後的高度,會平均分配在Content Area的上面跟下面(half-leading),
最後,line-height等於height,也就代表整個box的高度一致了。




參考資料:
https://stackoverflow.com/questions/8865458/how-do-i-vertically-center-text-with-css
https://stackoverflow.com/questions/8799164/how-does-line-height-vertically-center-text
https://www.slideshare.net/maxdesign/line-height

2018年4月29日 星期日

[Python Package] Mac OS安裝Matplotlib Basemap套件過程記錄

說明:
Basemap是python中畫平面地圖的套件,使用PROJ將地球的經緯度投影至平面上,
以下記錄安裝的過程。


做法:
  • 根據官網的指示安裝下述套件,大部分都用pip install。


    PROJ依據其網頁建議,使用Homebrew安裝,


    安裝的過程若遇到:
    Error: Xcode alone is not sufficient on xxx(mac os version).
    Install the Command Line Tools:
    xcode-select --install

    不要懷疑,下指令xcode-select --install,將會安裝所需的Command Line Tools。
    再下指令brew install proj。


    即完成PROJ安裝。

  • 接下來是GEOS,若沒安裝過,可用內含於basemap的geo-3.3.3版本,也可以自己下載最新版本。
  • Pillow沒用所以我沒裝。


  • 最後下載basemap的tar.gz版本。解壓縮後,開啓終端機cd到basemap主目錄。
  • 如果沒安裝過GEOS,再cd到geo-3.3.3目錄。
  • 下指令:export GEOS_DIR=/usr/local
  • 下指令:./configure --prefix=$GEOS_DIR
  • 下指令:make; make install
  • 若是安裝過GEOS,且geos_c.h在/usr/local/include下與libgeos_c在/usr/local/lib下,可參照下圖,則設定GEOS_DIR = /usr/local
  • 若GEOS不是裝在/usr/local,請自行設定GEOS_DIR。

  • 切回basemap主目錄
  • 下指令:python3 setup.py install (使用python3.6)
  • 下指令:python3 simpletest.py
  • 若出現:ModuleNotFoundError: No module named 'pyproj',代表需再安裝pyproj,可下指令pip3 install pyproj
  • 若以上指令皆執行成功,可執行example下的單一python檔確認(如:counties.py),若是執行run_all.py,會跑很多demo程式碼。

執行相關畫面如下圖。


參考資料:
https://matplotlib.org/basemap/users/installing.html
https://gis.stackexchange.com/questions/8120/importerror-no-module-named-pyproj/8136#8136

2018年3月31日 星期六

[.Net] HttpRuntime.Cache.Insert中absoluteExpiration與slidingExpiration可共存??

說明:
MSDN查HttpRuntime.Cache.Insert說明時,看到下面這段文字:

如果沒搞錯意思,應該是說在slidingExpiration設個參數(大於0),在absoluteExpiration設為DateTime.Now.Add(該參數),可以有如上說明的效果。

但不是說absoluteExpiration與slidingExpiration不能共存嗎???

有看沒有懂,實際測試看看(.Net framework 4.5.2 + MVC 5.2.3):


實際執行,出現ArgumentException,
看起來absoluteExpiration與slidingExpiration不能同時生效。
後來使用.Net framework4 + WebForm測試,也是一樣的Exception。


資料來源:
https://msdn.microsoft.com/en-us/library/4y13wyk9(v=vs.110).aspx