tag:blogger.com,1999:blog-18292132343333898142024-03-06T07:22:58.763+08:00after dusk, before dawn最近好像沒啥能期待的MMO....Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.comBlogger980125tag:blogger.com,1999:blog-1829213234333389814.post-43264901628998213942023-11-19T04:02:00.001+08:002023-11-19T04:35:52.781+08:00ExpressVPN STEAL my credit card info to renew subscription<p>Yes, it is ExpressVPN, one of top VPN in the world.</p><p>And it ExpressVPN steal my card info to renew subscription, let's see how it happen.</p><p>In short:<br />1. 2022/8/18, I purchased 1 year + 3 months plan($99.95) with credit card, Subscription ID 948*</p><p>2. The credit card expired in 2023.</p><p>3. 2023/11/17, my ExpressVPN plan expired.</p><p>4. It's Black Friday time, I compared few VPNs, give ExpressVPN another try, use new credit card to purchase 1 year + 3 months plan($99.95) again, Subscription ID 1039*</p><p>5. 2023/11/18, ExpressVPN use my new credit card info to renewing Subscription ID 948*, charged US$116.95.</p><p>I have to say, it was <b><span style="color: red;">STEAL</span></b>, <b><span style="color: #fcff01;">ExpressVPN steal my credit card info that for new subscription to renew another subscription without asking</span></b>.</p><p><br /></p>
<p>Ok let's see the detail.</p><div class="separator" style="clear: both; text-align: center;"><a href="https://i.imgur.com/2TQTz9W.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="631" data-original-width="1181" height="631" src="https://i.imgur.com/2TQTz9W.png" width="1181" /></a></div><br /><p>You can see, my first plan purchased at 2022/8/18, Subscription ID 948*.</p><p><br /></p><p>And my credit card used for Subscription ID 948* was expired, ExpressVPN sent a email notice me, but I didn't renew card info because ExpressVPN increased the price.<br /><br /></p><div class="separator" style="clear: both; text-align: center;"><a href="https://i.imgur.com/5rxIOTz.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="547" data-original-width="566" height="547" src="https://i.imgur.com/5rxIOTz.png" width="566" /></a></div><br />
<div>Time moved to 2023/11/17, my ExpressVPN goes offline, after check their web I know my plan was expired.</div><div>After doing some research, I decide give ExpressVPN another try, because I am lazy to uninstall and install new VPN software.</div><div>So I purchase the Black Friday deal, 1 year + 3 months plan for $99.95 with my new credit card, Subscription ID 1039*.</div><div><br /></div><div>The next day, 2023/11/18, I received an email said my ExpressVPN has renewing.</div><div class="separator" style="clear: both; text-align: center;"><a href="https://i.imgur.com/ePM09El.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="287" data-original-width="650" height="287" src="https://i.imgur.com/ePM09El.png" width="650" /></a></div><div><br /></div>It was weird, so I checked ExpressVPN's website to figure out what happed.<div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://i.imgur.com/njjJFC5.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="632" data-original-width="1363" height="632" src="https://i.imgur.com/njjJFC5.png" width="1363" /></a></div><br /><div><br /></div><div>I have two subscription ID, start with 948 (old one) and 1039(new one).</div><div><br /></div><div>Subscription ID 948* : expired at 2023/11/17, credit card expired so it can't renew.</div><div>Subscription ID 1039*: purchased at 2023/11/17 with new credit card.</div><div><br /></div><div>Next day, <b><span style="color: #fcff01;">I guess ExpressVPN system detect I have a working credit card, so it renew my old subscription without asking, using the new credit card info</span></b>.</div><div><br /></div><div>Subscription ID 948* : renew at 2023/11/18 with new credit card info.</div><div><br /></div><div>That's why I will say <span style="color: red;">ExpressVPN steal my card info to renew subscription</span>, ExpressVPN using my credit card to process unauthorized payment.</div><div><br /><div><br /></div><div>Finally, beware ExpressVPN didn't notice you they will store your credit card information, it only said "Your payment information is fully protected.".</div><div class="separator" style="clear: both; text-align: center;"><a href="https://i.imgur.com/tDxQoG0.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="643" data-original-width="732" height="643" src="https://i.imgur.com/tDxQoG0.png" width="732" /></a></div><br /><div>Yea, not even in their TOS, I checked ExpressVPN Terms of Service at 2023/11/19, it didn't mention it will store/save your credit card info. </div><div><br /></div><div>ExpressVPN is not bad, speed is ok, response time sometimes better than without VPN, I can play LOL with ExpressVPN and have lower ping, but I can't trust a company who steal your credit card info.</div><div><br /></div><div>I had ask customer service to refund my 2 subscriptions, hope it working or I have to issue VISA for suspicious transactions. </div><div><br /></div><div><br /></div><div><br /></div></div>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-72079654544467549342023-01-11T04:37:00.001+08:002023-01-11T06:41:27.066+08:00台灣想要的是和平,不是戰爭很久沒寫文章了,看到PTT上有篇標題叫做「[問卦] PTT投降仔都沒想過台灣打贏戰爭嗎?」,回了之後想說別浪費,就潤一下搬到blog來。<span></span><a href="http://www.died.tw/2023/01/blog-post.html#more">Read more »</a>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-32867707605103328412022-06-22T16:39:00.003+08:002022-06-28T16:31:33.582+08:00[.NET 6] Custom Converter for System.Text.JsonSome people asked a question about custom converter for System.Text.Json on Facebook .NET group.<br>
He want user get the specific format from DateTimeOffset (not DateTime)<br>
<pre class="prettyprint" style="font-size: 12px;"><code class="csharp">
DateTimeOffset.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fffzzz");
//"2022-06-22 06:28:55.207+00:00"
</code></pre>
<br>
If using default converter, the result will follow ISO 8601 format<br>
<pre class="prettyprint" style="font-size: 12px;"><code class="csharp">
return DateTimeOffset.UtcNow;
//"2022-06-22T05:55:57.4313761+00:00"
</code></pre>
<br>
And the don't want to use ToString() to achieve it everytime, so the best way is add the custom converter to JsonOptions<br>
<a href="http://www.died.tw/2022/06/net-6-customconverter-for-systemtextjson.html#more">Read more »</a>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-53664570997616402522022-01-03T18:51:00.002+08:002022-01-03T18:51:52.405+08:00WSL2 - Temporary failure in name resolutionWSL2 is good thing, but dunno why some bug it never fixed.<br />
<br />
If you are first time install WSL2 and reboot or something like it, it may become no internet connectivity and show error<br />
<blockquote>Temporary failure in name resolution</blockquote>
<br />
To fix it, please follow this gitgub issue, <a href="https://github.com/microsoft/WSL/issues/5256#issuecomment-666545999" target="_blank">JohnnyQuest1983's reply</a>.<br />
<br />
The steps can be simple like this<br />
<ol style="text-align: left;">
<li>set generateResolvConf=false in /etc/wsl.conf</li>
<li>set dns in /etc/resolv.conf</li>
</ol><br />
<br />
then use exit or wsl --shutdown, reboot wsl, and it worked, the internet back.Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-86313261939672234522021-10-14T23:09:00.002+08:002021-10-15T01:54:41.576+08:00[.NET 6] Implement custom LogProvider with RabbitMQ into ILogger<div><p>In .NET 6, ILogger is very easy to use, but on modern project, we will write log to somewhere else and use log viewer to read it. For example: sent log to <a href="https://www.rabbitmq.com/" target="_blank">RabbitMQ</a> and read on <a href="https://www.elastic.co/elastic-stack/" target="_blank">Kibana</a></p></div>
<div><p>Here is a demo to show how to lmplement a custom logging provider with RabbitMQ into ILogger in .NET 6 RC1, basically same as this <a href="https://docs.microsoft.com/en-us/dotnet/core/extensions/custom-logging-provider" target="_blank">article</a> but with some modify.</p></div>
<div><h3 style="text-align: left;"><span style="color: #93c47d;">Step 0 : Before Start</span></h3></div>
<div><p>In this demo we choose <a href="https://www.nuget.org/packages/RabbitMQ.Client/" target="_blank">RabbitMQ.Client</a> to communite with RabbitMQ, you can choose other client if you want, just install it from nuget, easy.</p></div>
<div><p>Another thing is RabbitMQ server, if you don't have one, you can use docker to create one.</p></div>
<div><pre class="prettyprint" style="font-size: 12px;"><code class="shell">docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.9-management</code></pre></div>
<br>
<a href="http://www.died.tw/2021/10/net-6-implement-custom-logprovider-with.html#more">Read more »</a>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-12845213329791064902021-09-28T21:55:00.003+08:002021-09-28T21:55:00.177+08:00[.NET 6] System.StackOverflowException after upgrade VS2022 to Preview 4.1 from 3.1<div class="separator" style="clear: both; text-align: center;"><a href="https://i.imgur.com/9wZvOZp.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="160" data-original-width="626" src="https://i.imgur.com/9wZvOZp.png" /></a></div><br /><div><br /></div><div>After upgrade VS2022 to Preview 4.1 from 3.1, my project throw StackOverflowException (after fix lots of reference error)</div><div><pre class="prettyprint" style="font-size: 12px;"><code class="cs">System.StackOverflowException
HResult=0x800703E9
Message=Exception of type 'System.StackOverflowException' was thrown.
Stack overflow.
Repeat 2291 times:
--------------------------------
at Microsoft.Extensions.Primitives.ChangeToken+ChangeTokenRegistration`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnChangeTokenFired()
at Microsoft.Extensions.Primitives.ChangeToken+ChangeTokenRegistration`1+<>c[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<RegisterChangeTokenCallback>b__7_0(System.Object)
at System.Threading.CancellationTokenSource.Invoke(System.Delegate, System.Object, System.Threading.CancellationTokenSource)
at System.Threading.CancellationTokenSource.Register(System.Delegate, System.Object, System.Threading.SynchronizationContext, System.Threading.ExecutionContext)
at System.Threading.CancellationToken.Register(System.Delegate, System.Object, Boolean, Boolean)
at System.Threading.CancellationToken.Register(System.Action`1<System.Object>, System.Object)
at Microsoft.Extensions.Configuration.ConfigurationReloadToken.RegisterChangeCallback(System.Action`1<System.Object>, System.Object)
at Microsoft.Extensions.Primitives.ChangeToken+ChangeTokenRegistration`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].RegisterChangeTokenCallback(Microsoft.Extensions.Primitives.IChangeToken)
--------------------------------
at Microsoft.Extensions.Primitives.ChangeToken+ChangeTokenRegistration`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnChangeTokenFired()
at Microsoft.Extensions.Primitives.ChangeToken+ChangeTokenRegistration`1+<>c[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<RegisterChangeTokenCallback>b__7_0(System.Object)
at System.Threading.CancellationTokenSource.Invoke(System.Delegate, System.Object, System.Threading.CancellationTokenSource)
at System.Threading.CancellationTokenSource+CallbackNode+<>c.<ExecuteCallback>b__9_0(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.CancellationTokenSource+CallbackNode.ExecuteCallback()
at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean)
at System.Threading.CancellationTokenSource.NotifyCancellation(Boolean)
at System.Threading.CancellationTokenSource.Cancel()
at Microsoft.Extensions.Configuration.ConfigurationManager.RaiseChanged()
at Microsoft.Extensions.Configuration.ConfigurationManager.AddSource(Microsoft.Extensions.Configuration.IConfigurationSource)
at Microsoft.Extensions.Configuration.ConfigurationManager.Microsoft.Extensions.Configuration.IConfigurationBuilder.Add(Microsoft.Extensions.Configuration.IConfigurationSource)
at Microsoft.Extensions.Configuration.ChainedBuilderExtensions.AddConfiguration(Microsoft.Extensions.Configuration.IConfigurationBuilder, Microsoft.Extensions.Configuration.IConfiguration, Boolean)
at Microsoft.Extensions.Configuration.ChainedBuilderExtensions.AddConfiguration(Microsoft.Extensions.Configuration.IConfigurationBuilder, Microsoft.Extensions.Configuration.IConfiguration)
at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
at Program.<Main>$(System.String[])</code></pre></div><div><br /></div>
<div>I know post to MS's developer forum is useless, and VS2022 can't downgrade now, so the only way is try and error by myself.</div><div><br /></div><div>Turn out, the stackoverflow exception cause by <b>Microsoft.Extensions.Configuration </b>from this code:</div>
<div><pre class="prettyprint" style="font-size: 12px;"><code class="cs">builder.Services.AddSingleton<IConfiguration>(builder.Configuration);</code></pre></div><div>After more dig-in, I found the framework already called this on <a href="https://github.com/dotnet/aspnetcore/blob/59baa0d6f93022e87cc82faaf0433fe3513a38a9/src/Hosting/Hosting/src/WebHostBuilder.cs#L290" target="_blank">Hosting</a> , so maybe the duplicate call cause the stackoverflow exception, but still not sure why it worked on VS2022 preview 3.1, not work on preview 4.1.</div><div><br /></div>
<h3 style="text-align: left;"><span style="color: #93c47d;">Solution</span></h3><div><br /></div><div>Remove the line</div>
<div><pre class="prettyprint" style="font-size: 12px;"><code class="cs">services.AddSingleton<IConfiguration>(configuration);</code></pre></div>
<div>(or similar) on your Program.cs</div>
Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-65853845727877449652021-09-22T21:13:00.003+08:002021-09-22T21:13:00.222+08:00[JAVA] HMAC SHA384 example codeIt was a bit hard to find correct HMAC SHA384 code for JAVA, so here to share my example code, hope can save some time to try and error.
<div><pre class="prettyprint" style="font-size: 12px;"><code class="java">
import java.security.NoSuchAlgorithmException;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Mac;
public class SecureUtils {
private static String bytesToHex(final byte[] hash) {
final StringBuffer hexString = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
final String hex = Integer.toHexString(0xff & hash[i]);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
public static void main(String[] args) throws NoSuchAlgorithmException {
final String nonce = "test"; //your key
final String message = "Password"; //your plain txt
SecretKeySpec signingKey = new SecretKeySpec(nonce.getBytes(),"HmacSHA384");
final Mac mac = Mac.getInstance("HmacSHA384");
try{
mac.init(signingKey);
System.out.println(bytesToHex(mac.doFinal(message.getBytes())));
}
catch(Exception ex){ }
}
}</code></pre></div><div><br /></div>
You can test this example in <a href="http://tpcg.io/KUN888" target="_blank">here</a> <div><br /></div><div>Check result with <a href="https://www.freeformatter.com/hmac-generator.html" target="_blank">online hmac generator</a></div><div><br /></div><div>Those code modify from stackoverflow <a href="https://stackoverflow.com/questions/45927378/hmac-sha-384-in-java" target="_blank">HMAC SHA-384 in Java</a></div>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-23112097066050644512021-01-31T11:15:00.001+08:002021-02-01T11:34:09.153+08:00 [敗家] Alienware m17 R3<blockquote class="imgur-embed-pub" data-id="a/Lu9PmBI" lang="en"><a href="//imgur.com/a/Lu9PmBI">Alienware m17 R3</a></blockquote><script async="" charset="utf-8" src="//s.imgur.com/min/embed.js"></script>
<br />
上一台筆電是2013年買的<a href="https://www.died.tw/2012/10/alienware-m14x-r2.html" target="_blank">Alienware M14x R2</a>,拿來寫code已經有點慢了,最近終於找了機會買了新筆電,17吋螢幕真的比較適用啊。<br />
<br />
不能免俗地附上配備表。<br />
<div class="separator" style="clear: both;"><a href="https://i.imgur.com/SBtW1Zf.png" style="display: block; padding: 1em 0px; text-align: center;"><img alt="" border="0" data-original-height="800" data-original-width="792" height="640" src="https://i.imgur.com/SBtW1Zf.png" width="634" /></a></div><br />
<br />
順帶一提,這次順便買了外星人背包,裝17吋筆電剛好,還有些空間可以裝我需要的東西,相當不錯。<br />
<div class="separator" style="clear: both;"><a href="https://i.imgur.com/7z12hF0.jpeg" style="display: block; padding: 1em 0px; text-align: center;"><img alt="" border="0" data-original-height="800" data-original-width="450" height="800" src="https://i.imgur.com/7z12hF0.jpeg" width="450" /></a></div><br />
<br />
這是2020/12月底買的,沒想到一忙就忘記貼文,直到現在。
Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-48427523230303904162020-11-08T11:18:00.001+08:002020-11-08T11:18:00.605+08:00[.NET Core] 400 Bad Request / 'MS-ASPNETCORE-TOKEN' does not match the expected pairing token<p>At this time, I am testing an old code build by .NET Core 2.0, when I tring to send request to the service, I always got 400 Bad Request, but I can know the service got request because I am running debug mode, it shows request coming and exception happend.</p>
<br />
<p>Since I can't find out the error just by 400 bad request, I open "Application Insights Search" in Visual Studio and found this error.</p>
<div class="separator" style="clear: both;"><a href="https://i.imgur.com/IYEY3HE.png" style="display: block; padding: 1em 0px; text-align: center;"><img alt="" border="0" data-original-height="99" data-original-width="769" src="https://i.imgur.com/IYEY3HE.png" /></a></div>
<p>After some digging on stackoverflow, I found the error cause by a old version bug.</p>
<br />
<p>This question "<a href="https://stackoverflow.com/questions/38839034/cannot-debug-in-visual-studio-after-changing-the-port-number" target="_blank">Cannot debug in Visual Studio after changing the port number?</a>" 's answer has good explain about this error.</p>
<blockquote>You are getting this error message because Kestrel expected to run behind IIS, and received a direct request. And it noticed that because IIS was not there to inject the MS-ASPNETCORE-TOKEN header.
--Gerardo Grignoli</blockquote>
<br />
<p>Here have some options to solve the problem:</p>
<ol style="text-align: left;">
<li> Call UseUrls() before UseIISIntegration() </li>
<li> Use Kestrel to debug, not IIS Express </li>
<li> Upgrade the framework</li></ol>
<br/>
<p>
That's all,</p>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-68955755225873870612020-11-01T15:43:00.006+08:002020-11-01T15:43:02.715+08:00[.NET core] Localization with EnumerationIn .NET core, doing i18n is more easy than .NET Framework in my opinions, just give appropriate setting, then you can easily assign i18n string to anyplace you want.<br /><br />
Let's see how it work.<div><br /><h4 style="text-align: left;">
1.Add resource file </h4><div>Usually I will put my resource file in <span style="color: #3d85c6;">[Resources]</span> folder, remember put cultureInfo name on resx file like this.<br />
</div></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9KmpqdJbuPXSDp9Yi5CQLP-TwEJZ8TRGGL3BZp24UmXzu-5vXPCSD4j2yWbMGWV4mah0jcN8iR78BO5Zi1zGLr5eFd0IziDXATjWgCOLsG1lfz3Te454HDQ5KymC5L-ZGfogndT-91Y4r/s212/68747470733a2f2f692e696d6775722e636f6d2f58525145624e612e706e67.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="114" data-original-width="212" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9KmpqdJbuPXSDp9Yi5CQLP-TwEJZ8TRGGL3BZp24UmXzu-5vXPCSD4j2yWbMGWV4mah0jcN8iR78BO5Zi1zGLr5eFd0IziDXATjWgCOLsG1lfz3Te454HDQ5KymC5L-ZGfogndT-91Y4r/s0/68747470733a2f2f692e696d6775722e636f6d2f58525145624e612e706e67.png" /></a></div><br /><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div>
<h4 style="text-align: left;">2.Setup Startup.cs</h4>
Add following code (depending how many language you support) into your IServiceCollection <br/>
<pre class="prettyprint" style="font-size: 12px;"><code class="cs">//set Support Cultures and default
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]{
new CultureInfo("en-US"),
new CultureInfo("zh-TW"),
new CultureInfo("ja-JP")
};
options.DefaultRequestCulture = new RequestCulture("en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
//set resource path
services.AddLocalization(options => options.ResourcesPath = "Resources");
</code></pre><br />
Make sure your ResourcesPath set to right path.<br/>
<br /><br />
<h4 style="text-align: left;">3.DI Localizer</h4>
Since we are using .NET Core, so we need to DI our Localizer before we using it, for example if you are use it on a controller.<br/>
<pre class="prettyprint" style="font-size: 12px;"><code class="cs">private readonly IStringLocalizer _localizer;
public YourController(IStringLocalizerFactory factory)
{
_localizer = factory.Create("Message", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
}
</code></pre><br />
The "Message" is my resource name, you can DI multi Localizer if you have not only one resource group.<br/>
<br /><br />
<h4 style="text-align: left;">4.Usage</h4>
<div class="separator" style="clear: both;"><a href="https://i.imgur.com/Wwywfip.png" style="display: block; padding: 1em 0; text-align: center; "><img alt="" border="0" data-original-height="322" data-original-width="358" src="https://i.imgur.com/Wwywfip.png"/></a></div>
Here is the content of my resource, the usage for Localizer is easy, just use it like a dictionary.<br/>
<pre class="prettyprint" style="font-size: 12px;"><code class="cs">var str = _localizer["Cool"];
</code></pre><br /><br />
If you wanna get a localization string for enum, it was easy too, for example your enum called "WeatherSummary".<br/>
<pre class="prettyprint" style="font-size: 12px;"><code class="cs">var str = _localizer[WeatherSummary.Cool.ToString()];
</code></pre><br />
It was very simple, don't need add custom thing like LocalizedDescriptionAttribute for enum i18n in .NET Framework.<br/>
<br/><br/>
You can check my demo code on github <a href="https://github.com/died/.NET-Core-Localization-and-Enum" target="_blank">.NET-Core-Localization-and-Enum</a><br/><br/>
Hope you enjoy it.<br/>
Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-36958903343304781692020-10-25T18:04:00.001+08:002020-10-25T18:04:00.301+08:00[.NET core] System.NotSupportedException: The collection type 'System.Object' on 'somewhere' is not supported. Somehow if you are using .NET core and return a object on controller, you might meet this exception.<div> <pre class="prettyprint" style="font-size: 12px;"><code class="cs">
System.NotSupportedException: The collection type 'System.Object' on 'somewhere.InnerResult.Data' is not supported.
at System.Text.Json.JsonPropertyInfoNotNullable`4.GetDictionaryKeyAndValueFromGenericDictionary(WriteStackFrame& writeStackFrame, String& key, Object& value)
at System.Text.Json.JsonPropertyInfo.GetDictionaryKeyAndValue(WriteStackFrame& writeStackFrame, String& key, Object& value)
at System.Text.Json.JsonSerializer.HandleDictionary(JsonClassInfo elementClassInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteCore(Utf8JsonWriter writer, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCore(PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCoreString(Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
at TestWeb.Controllers.AuthController.VerifyToken(String token, Int32 tp) in C:\repos\somewhere\TestWeb\Controllers\AuthController.cs:line 51</code></pre><br /></div>
<div>In this project, I always return my custom result called InnerResult in API controller.</div>
<pre class="prettyprint" style="font-size: 12px;"><code class="cs"> public class InnerResult
{
public bool Success { get; set; } = true;
public int Code { get; set; }
public string Message { get; set; }
public dynamic Data { get; set; }
}
</code></pre><br />
<div>If the result json was this format, .NET core default can handle it.</div>
<pre class="prettyprint" style="font-size: 12px;"><code class="cs">{
"success": true,
"code": 0,
"message": null,
"data": "vfu3MF5RFlQV9dWNQCFh6iRPNxeezFaV"
}</code></pre><br />
<div>but if some object in property, it will throw System.NotSupportedException</div><br />
<pre class="prettyprint" style="font-size: 12px;"><code class="cs">{
"success": true,
"code": 0,
"message": null,
"data": {
"name": "Died"
}
}</code></pre><br />
That's because in .NET core , they default using <a href="https://docs.microsoft.com/en-US/dotnet/api/system.text.json?view=netcore-3.1">System.Text.Json</a> to deal with json convert, but somehow it can't handle object in property, so......
<div><br /></div>
<div>To solve it, we can use our old friend Json.NET to save the day. (actually, using <a href="https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.NewtonsoftJson/" target="_blank">Microsoft.AspNetCore.Mvc.NewtonsoftJson</a>)</div>
<div><br /></div>
<blockquote><div>Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.1.9</div></blockquote>
<p>If you are using .NET core 2.x , add <span class="pun" style="box-sizing: border-box; color: #e8e6e3; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 15px;">.</span><span class="typ" style="box-sizing: border-box; color: #5cbbd7; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 15px;">AddNewtonsoftJson</span><span class="pun" style="box-sizing: border-box; color: #e8e6e3; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 15px;">()</span> after your AddMVC()</p>
<p></p>
<blockquote>services.AddMvc().AddNewtonsoftJson();</blockquote>
<p></p>
<p>If using .NET core 3.x , add it after what you have.</p>
<blockquote><p>services.AddControllers().AddNewtonsoftJson();<br/>
services.AddControllersWithViews().AddNewtonsoftJson();<br/>
services.AddRazorPages().AddNewtonsoftJson();</p></blockquote>
<p>Then no more this System.NotSupportedExceptio again.</p><p><br /></p>
<p><br /></p>
Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-54962769000377170962020-10-18T12:26:00.016+08:002020-10-20T17:17:29.175+08:00[.NET core] AddPolicy to Authorize on MVC controllerSometimes when you are using C# identity, the [Authorize] attribute is not enough,for example, you want additional check not only claims or role, then you can try add policy to make it. <div><br /></div>
<div>In this example, we assume a part of method need check token in user claims before access, here is how to implement it. </div><div><br /></div>
<div>1.add a AuthorizationHandler for your policy </div>
<div><code> Handler/AuthorizeTokenHandler.cs</code>
<pre class="prettyprint" style="font-size: 12px;"><code class="cs">namespace Example.Handler
{
// Custom AuthorizationHandler for check token in claim
public class AuthorizeTokenHandler : AuthorizationHandler<TokenRequirement>
{
private readonly AuthService _auth;
public AuthorizeTokenHandler(AuthService auth)
{
_auth = auth;
}
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TokenRequirement requirement)
{
//no token claim
if (!context.User.HasClaim(x => x.Type == "Token"))
{
context.Fail();
return Task.CompletedTask;
}
var username = context.User.FindFirst(ClaimTypes.Name).Value;
var token = context.User.FindFirst("Token").Value;
#region check token
if (_auth.CheckToken(username,token))
{
context.Succeed(requirement);
}
else
{
context.Fail();
}
#endregion
return Task.CompletedTask;
}
}
public class TokenRequirement : IAuthorizationRequirement
{
}
</code></pre><br />
</div>
<div>2. add to startup, in ConfigureServices</div><br />
<pre class="prettyprint" style="font-size: 12px;"><code class="cs"> //handler
services.AddSingleton<IAuthorizationHandler, AuthorizeTokenHandler>();
//add policy
services.AddAuthorization(options =>
{
options.AddPolicy("TokenRequire", policy =>
policy.Requirements.Add(new TokenRequirement()));
});
</code></pre><br />
<div>3. apply policy to Authorize when you need</div><br />
<pre class="prettyprint" style="font-size: 12px;"><code class="cs">[Authorize(Policy = "TokenRequire")]
[HttpPost]
public ApiResult SomeMethod()
{
}
</code></pre><br />
<div>That's all, enjoy it.</div><br />Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-40531725133808204292020-10-11T22:38:00.002+08:002020-10-20T12:52:55.241+08:00[C#] Best Practices of Get Claim from IdentityIf you are using ASP.NET Identity, whatever using <a href="https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-3.1&tabs=visual-studio" target="_blank">ASP.NET Core Identy</a> or <a href="https://docs.microsoft.com/en-us/aspnet/identity/overview/getting-started/introduction-to-aspnet-identity" target="_blank">ASP.NET Identity</a>, usually you will add some claims into the identity and use it later, but I saw lot people using unsafe way to get claim value from identity, it may cause uncatch error if the claim type not exists, let's see the code.
<pre class="prettyprint" style="font-size: 12px;"><code class="cs">//set some claims first
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name,"test name"),
new Claim("Token","token123"),
new Claim("Number","3")
//new Claim("dummy","dummy string")
};
var ci = new ClaimsIdentity(claims);
//most seen way, throw error if not exists
var name = ci.Claims.First(x => x.Type == ClaimTypes.Name).Value;
var token = ci.Claims.First(x => x.Type == "Token").Value;
//using FirstOrDefault() only, throw error if not exists
//var dunno = ci.Claims.FirstOrDefault(x => x.Type == "dunno").Value;
</code></pre><br />
Using this way is easy, but if the claim not exists, if will cause error, whatever you are using First() or FirstOrDefault().
If want to using FirstOrDefault() to get claim value, you can using this fixed way.
<pre class="prettyprint" style="font-size: 12px;"><code class="cs">
//fixed FirstOrDefault way: return null if not exists
var dunno = ci.Claims.Where(x => x.Type == "dunno").Select(x => x.Value).FirstOrDefault();
</code></pre><br />
but you can use <a href="https://docs.microsoft.com/en-us/dotnet/api/system.security.claims.claimsidentity.hasclaim?view=netcore-3.1" target="_blank">HasClaim</a> method to check claim exist first, and give a proper value if it not exists.
<pre class="prettyprint" style="font-size: 12px;"><code class="cs">
//safe way: using HasClaim to check
var dummy = ci.HasClaim(x => x.Type == "dummy")
? ci.Claims.First(x => x.Type == "dummy").Value
: null;
</code></pre><br />
or you can choose this short way by using <a href="https://docs.microsoft.com/en-us/dotnet/api/system.security.claims.claimsidentity.findfirst?view=netcore-3.1" target="_blank">FindFirst</a> method.
<pre class="prettyprint" style="font-size: 12px;"><code class="cs">
//short way: using FindFirst
var dummyShort = ci.FindFirst("dummy")?.Value;
//short way for int
int.TryParse(ci.FindFirst("Number")?.Value, out var number);
</code></pre><br />
I think use ClaimsIdentity.FindFirst Method is the best practices for get claim value.
<br>
You can test those here in <a href="https://dotnetfiddle.net/g9UmI1" target="_blank">here</a>
<iframe width="100%" height="600" src="https://dotnetfiddle.net/Widget/g9UmI1" frameborder="0"></iframe>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-65409452543795861362020-06-02T20:29:00.000+08:002020-06-02T20:35:52.408+08:00幾個關於熊的謠言最近在網路上發現有人對於熊有些錯誤觀點,這很可能造成真正遇到熊時的人員傷亡,所以找了一下資料,想來澄清一下這些謠言/迷思。<br>
<br>
以下資料來源翻譯自<a href="https://yellowstonebearworld.com/">Yellowstone Bear World</a> <br>
<br>
<h4>謠言#1:熊的視力很差</h4><a href="https://yellowstonebearworld.com/myth-1-bears-have-bad-eyesight">Myth #1: Bears have bad eyesight</a><br>
<br>
這確實是一個常見的錯誤認知。是的,熊確實有驚人的嗅覺,但牠的嗅覺並不是彌補視力的不足,實際上牠們具有很好的視力。 <br>
<br>
<div class="separator" style="clear: both; text-align: center;float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" src="https://i.imgur.com/MiShtBZ.png" data-original-width="300" data-original-height="166" alt="Bears actually have excellent eyesight." title="Bears actually have excellent eyesight."></div>熊在白天的視力與人類相同,但是到了夜晚,牠們出色的視覺才顯出了作用。<br>
<br>
就像您家的狗或貓一樣,熊擁有出色的夜視能力。 牠們的眼睛後部有一個稱為脈絡膜層(Tapetum lucidum)的反射膜,該膜可反射光,並使光敏細胞對光進行第二次反應,從而極大地增強了他們在夜間的視力。 <br>
<br>
這就是為什麼如果您在晚上看到牠們的照片,牠們的眼睛看起來會發綠。<br>
<br>
因此,別被騙了...那些熊可能會在您見到牠們之前先見到您! (有關資訊請參見Sylvia Dolson的<a href="https://amzn.to/36WNQ3f">《熊學 Bear-ology》</a>)<br>
<br>
<a href="http://www.died.tw/2020/06/blog-post.html#more">Read more »</a>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-10055039758198057512019-12-12T14:58:00.000+08:002019-12-12T14:58:42.251+08:00少女韓國瑜:環保少女 Greta Thunberg<div class="separator" style="clear: both; text-align: center;"><a href="https://i.imgur.com/OfEhXbk.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="800" data-original-width="584" src="https://i.imgur.com/OfEhXbk.png"></a></div><br>
今年Times的年度人物,是來自瑞典的環保少女Greta Thunberg,就先不說她不在投票前三名,得票不到4%是怎麼被操作選上的這種拉基事了,很多人都從媒體片面了解這個人,覺得她很環保很有知識很有行動力啥的,但其實大部分人都被騙了,她就是個少女韓國瑜而已。<br>
<br>
<a href="http://www.died.tw/2019/12/greta-thunberg.html#more">Read more »</a>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-612893260906519322019-01-10T13:17:00.000+08:002019-01-10T13:17:15.999+08:00[C#] YOLO3 with OpenCvSharp4<img border="0" data-original-height="950" data-original-width="1600" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiUVTE2RX6NRDw2O8BTF7Mk-L2RWsnJDeTqy5J0x4S7FT-rx1aiRmFC9NFyfxYuu2hYq7XkqlEsndMe38G8hAtiaZglI9F80CF-To_Po2-kLbPn0pnAhc11CExIteyuwge5NTgIlf-h71V/s1600/Yolo3_Logo.png" width="100%"><br>
<br>
One year ago, I writed <a href="https://www.died.tw/2018/01/c-opencvsharp-dnn-with-yolo2.html">[C#] OpenCvSharp DNN with YOLO2</a>, since YOLO3 come out for a while and OpenCV released version 4 on Dec 2018, I think it's time to make a upgrade.<br>
<br>
<a href="http://www.died.tw/2019/01/c-yolo3-with-opencvsharp4.html#more">Read more »</a>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com3tag:blogger.com,1999:blog-1829213234333389814.post-84516536274030399522018-11-21T11:24:00.000+08:002018-11-21T15:41:15.844+08:00[C#] Swashbuckle WebAPI HTTPS BaseUrl and Bearer token 因為<a href="https://github.com/domaindrivendev/Swashbuckle">Swashbuckle</a>作者跑去專心做Swashbuckle.AspNetCore了,所以在使用上遇到一些問題時要自己想辦法解,這邊紀錄一下遇到的兩個問題。<br>
<br>
<br>
<h4>HTTPS BaseUrl Issue</h4>因為我有用 cloudflare 的 Flexible SSL,在production環境上外面看起來是HTTPS,但Server上走的仍然是HTTP,這時候swagger的 BaseUrl 會因為 Uri Scheme 出問題,所以要來想辦法解,整理需求後,我只需要本機測試(localhost)走HTTP,其他就走HTTPS,按照這個需求來做的解法如下。<br>
<br>
<pre class="prettyprint" style="font-size: 12px;"><code class="cs">//SwaggerConfig.cs
config.EnableSwagger(c =>
{
c.RootUrl(ResolveBasePath);
c.Schemes(new[] { "http", "https" });
}
internal static string ResolveBasePath(HttpRequestMessage message)
{
//fix for Cloudflare Flexible SSL and localhost test
var scheme = message.RequestUri.Host.IndexOf("localhost") > -1 ? "http://" : "https://";
return scheme + message.RequestUri.Authority;
}
</code></pre><br>
這樣就可以兼顧正式機與本機開發都正常使用,如果透過proxy那要再另外解,我這邊就先略過。<br>
<br>
<a href="http://www.died.tw/2018/11/c-swashbuckle-webapi-https-baseurl-and.html#more">Read more »</a>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-80163528922564820562018-10-24T15:42:00.000+08:002018-10-24T15:42:56.936+08:00[ASP.NET MVC] Remove response header | 移除回應標頭 <br />
有時候做網站會想把一些header移除,但是久久做一次都要查一下,所以決定寫這篇之後就不用查了。<br />
<br />
<img border="0" data-original-height="207" data-original-width="531" src="https://i.imgur.com/XSCv8Kd.png" /><br />
<br />
目前使用的版本是MVC 5.2.4,跑在IIS 10上,上面可以看到沒設定前,response有三個header是一般來說大家會去移除的,分別是server, x-aspnet-version, x-powered-by,下面分別說一下移除方法,我是找不用動到IIS設定,改動最小的做法,但是需要改到web.config,如果權限不能碰到web.config的話要另外想辦法。<br />
<br />
<h4><span style="color: #6aa84f;">Server</span></h4>移除Server header需要在 Global.asax.cs 裡面加上這段<br />
<pre class="prettyprint" style="font-size: 12px;"><code class="cs"> protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
var app = sender as HttpApplication;
// remove Server header
app?.Context.Response.Headers.Remove("Server");
}
</code></pre>不過這樣子只能移除由程式產生檔案的header,對於靜態檔像是.js .css之類的還是會有header,所以還有另外一個地方要修改;在web.config內的system.webServer元素內,將 modules 加入runAllManagedModulesForAllRequests="true"這個設定,像是下面這樣,就可以把靜態檔的header也移掉。<br />
<img border="0" src="https://i.imgur.com/OWZ8aA9.png" data-original-width="529" data-original-height="54" /><br />
<br />
<h4><span style="color: #6aa84f;">X-AspNet-Version</span></h4>要移除這個header要修改web.config,在httpRuntime element內加入enableVersionHeader="false",像我的情況就是下面這樣。<br />
<img border="0" data-original-height="193" data-original-width="652" src="https://i.imgur.com/12xZc5W.png" /><br />
<br />
<br />
<h4><span style="color: #6aa84f;">X-Powered-By</span></h4>這header也要在web.config內修改,在system.webServer元素內加入customHeaders,像是下面這樣<br />
<img border="0" data-original-height="214" data-original-width="359" src="https://i.imgur.com/XaSEIa8.png" /><br />
<br />
<br />
<br />
<br />
最後再看看結果,上面三個header都移除了,可賀可喜,收工。<br />
<img border="0" data-original-height="163" data-original-width="409" src="https://i.imgur.com/HhtG84T.png" />Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-51420391116122239962018-10-23T13:25:00.000+08:002018-10-24T11:24:54.663+08:00[C#] XML to Object / Generic | XML泛型轉換Recently, I have to some things related to XML in the work, so I prepared some tools and wrote the method of converting XML to Object, but it is not very beautiful on using.<br>
最近工作上需要做一些跟XML有關的東西,所以先準備些工具,一開始也沒多想就寫了將XML轉成Object的方法,但是使用上有點不漂亮。<br>
<pre class="prettyprint" style="font-size:12px"><code class="cs">static object XmlToObj(XDocument xml, Type objType)
{
object obj;
try
{
XmlSerializer serializer = new XmlSerializer(objType);
using (TextReader reader = new StringReader(xml.ToString()))
{
obj = serializer.Deserialize(reader);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
return obj;
}
</code></pre><br>
Using above XmlToObj, it need to cast it first, the code isn't feel so beauty, so I decide rewrite it to generic method.<br>
使用上面的XmlToObj,會需要做轉型才能使用,code感覺不是很美,所以我決定改寫成泛型的方式。<br>
<pre class="prettyprint" style="font-size:12px"><code class="cs">var result = (PurchaseOrder)XmlToObj(doc, typeof(PurchaseOrder));
</code></pre><br>
<a href="http://www.died.tw/2018/10/c-xml-to-object-generic-xml.html#more">Read more »</a>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-16593521704690711502018-06-06T16:51:00.000+08:002018-06-06T17:00:32.537+08:00[C#] Implement Pivot with LINQ | 用 LINQ 實現 PivotHere we will demo how to using LINQ implement Pivot feature in two ways, at first we need a demo data source.<br>
這邊我們要展示如何使用LINQ實作Pivot功能,共兩個方式,首先我們需要一個示範的資料來源。<br>
<br>
<a data-flickr-embed="true" href="https://www.flickr.com/photos/doomdied/42561632362/in/dateposted-public/" title="pivot_1"><img alt="pivot_1" height="659" src="https://farm2.staticflickr.com/1747/42561632362_8ea3c4aba1_o.png" width="306"></a><br>
That table with two brand(both having two products)in four months will be use in this demo.<br>
這個有兩個品牌(各有兩個產品)在四個月間的表格將會用在這次demo.<br>
<a href="http://www.died.tw/2018/06/c-implement-pivot-with-linq-linq-pivot.html#more">Read more »</a>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-86216783283962233252018-05-25T15:11:00.002+08:002018-05-25T15:13:21.807+08:00政府錯了,機車才是最乾淨的交通工具<span style="color: #666666; font-size: small;">[At first,這邊所謂的交通工具,定義為使用燃油(汽油、柴油)的交通工具。]</span><br>
<br>
五月中時,新任總統府祕書長陳菊表示「<a href="http://www.cna.com.tw/news/aipl/201805160005-1.aspx">新北市機車全台最多 汙染超過深澳電廠</a>」,言下之意就是機車很汙染就對了。<br>
<br>
政府每次都拿最弱勢的機車開刀早就不意外了,之前當上行政院長的賴清德「<a href="https://www.thenewslens.com/article/86022">抗空汙</a>」也是先砍機車再說,反正政府與論就是讓民眾覺得機車汙染很大,但事實真的是這樣嗎 ?<br>
<br>
<b><span style="color: red;">錯,在汽油車、柴油車、機車三者中,機車是最乾淨的。</span></b><br>
<br>
以<span style="color: yellow;">平均單輛總排放</span>來看,<span style="color: yellow;">機車不到汽油車的二分之一,更不到柴油車的十六分之一</span>,若只看<span style="color: yellow;">PM2.5排放,單輛汽油車平均是機車的三倍,柴油車則是將近機車的七十八倍</span>。<br>
<iframe frameborder="0" height="450.89916666666664" scrolling="no" seamless="" src="https://docs.google.com/spreadsheets/d/e/2PACX-1vQFBoqcV_bW8vwOYH7s4pFtNhJZ9ZRUDjk1bnlWkpKU_UL3RuvCsnKbblGFIG8lhz9miajYI1DalK9s/pubchart?oid=5699429&format=interactive" width="785.8457207207207"></iframe><br>
<br>
<a href="http://www.died.tw/2018/05/blog-post.html#more">Read more »</a>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com15tag:blogger.com,1999:blog-1829213234333389814.post-3162130460834714072018-04-03T11:59:00.002+08:002019-07-03T22:05:24.954+08:00[C#] An fast way to implement Active Directory login to ASP.NET MVCAlthough OWIN have a package "<a href="https://www.nuget.org/packages/Microsoft.Owin.Security.ActiveDirectory/">Microsoft.Owin.Security.ActiveDirectory</a>" seems can support AD login, but it looks like made for ADFS, and <a href="https://msdn.microsoft.com/zh-tw/library/microsoft.owin.security.activedirectory.activedirectoryfederationservicesbearerauthenticationoptions(v=vs.111).aspx">leak document</a> on MSDN so I dunno how to use it on basic AD login, so I decide find other way to implement AD login to MVC.<br>
<br>
After long long time search, this post "<a href="https://stackoverflow.com/questions/33180687/owin-with-ldap-authentication">OWIN with LDAP Authentication</a>" help me a lot, my solution basic on it and add some other feature.<br>
<br>
<a href="http://www.died.tw/2018/04/c-fast-way-to-implement-active.html#more">Read more »</a>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-10942306230167815182018-03-19T13:33:00.000+08:002018-03-19T13:33:20.571+08:00[PowerShell] Download file and unzip | 下載檔案與解壓縮PowerShell is an useful command tool on Windows, here I will show how to download file and unzip it.<br />
PowerShell是個在Windows上很好用的指令列工具,這邊我將展示如何下載檔案和解壓縮。<br />
<br />
<h3><span style="color: #b6d7a8;">Download | 下載</span></h3>We using <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6">Invoke-WebRequest</a> to download file in Windows(like wget in linux), the command like this:<br />
我們使用<a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6">Invoke-WebRequest</a>來在Windows下載檔案(像linux的wget),指令如下:<br />
<pre><span style="color: #3d85c6;">Invoke-WebRequest</span> <span style="color: #e06666;">-Uri</span> <Uri> <span style="color: #e06666;">-OutFile</span> <String></pre><br />
By default PowerShell uses TLS 1.0, if your file host's SSL using TLS 1.2, you will got this this error:<br />
PowerShell預設使用TLS 1.0,如果你檔案網站的SSL使用TLS 1.2,你會遇到下面這個錯誤:<br />
<pre><span style="color: red;">Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel.</span></pre><br />
For this case, you can set PowerShell use TLS 1.2 first to avoid the error by this way.<br />
在這情況,你可以用下面方式設定PowerShell優先使用TLS 1.2來避免這個問題。<br />
<pre><span style="color: #cccccc;">[Net.ServicePointManager]</span>::SecurityProtocol = <span style="color: #6aa84f;">"tls12, tls11, tls"</span>
<span style="color: #3d85c6;">Invoke-WebRequest</span> <span style="color: #e06666;">-uri</span> https://died.github.io/us-map.zip <span style="color: #e06666;">-outfile</span> map.zip</pre><br />
<br />
<h3><span style="color: #b6d7a8;">Unzip | 解壓縮</span></h3>The command <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/expand-archive?view=powershell-6">Expand-Archive</a> can help us unzip file, usage like this:<br />
指令<a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/expand-archive?view=powershell-6">Expand-Archive</a>可以幫我們解壓縮檔案,使用方式如下:<br />
<pre><span style="color: #3d85c6;">Expand-Archive</span> <ZipFile> <span style="color: #e06666;">-DestinationPath</span> <String></pre><br />
Example | 範例 :<br />
<pre><span style="color: #3d85c6;">Expand-Archive</span> .\map.zip <span style="color: #e06666;">-DestinationPath</span> .\map\</pre><br />
Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-58112684728233595222018-03-16T14:44:00.000+08:002018-03-16T14:44:08.978+08:00從近年立委、議員選舉數據來看女性參政與保障名額台灣是少數國家將婦女保障名額寫入憲法內的國家,但那是1946年的事了,現在已經過了70多年,在當時看來相當需要的情況下產生的制度,是否在現今環境也需要,我找了近二十年的議員與立委選舉數據作為比較,來看看保障名額在現今社會對於選舉的影響。<br />
<br />
資料來源:<a href="http://db.cec.gov.tw/histMain.jsp">中選會資料庫網站</a><br />
資料選擇:直轄市區域議員1998~2015,區域立委2001~2016,各五筆<br />
<br />
會選擇直轄市區域議員與區域立委的原因是因為剛好前者是有保障名額的,而後者沒有,所以用兩者去比較是蠻恰當的。<br />
<br />
經整理數據後做成下面兩個圖表。<br />
<br />
<h3><span style="color: #6aa84f;">直轄市區域議員男女比例</span></h3><iframe frameborder="0" height="444.27250000000004" scrolling="no" seamless="" src="https://docs.google.com/spreadsheets/d/e/2PACX-1vQoQe7JWy1K4VUznvWdx1IYmjuPFKid8RK7lgZLx-pp0KPWLaB78gMcPJZ9kvDuxat-pcQQzjBXj1AT/pubchart?oid=1595665880&format=interactive" width="718.5"></iframe><br />
在這張表中,我們可以看到<br />
<ol><li>女性參選比例從20%逐年提升到31.25%,增加11.25個百分點,提升幅度為51.25%</li>
<li>女性當選率比男性當選率高出9.76至19.05個百分點,以平均值來說,<span style="color: yellow;">女性當選率高出男性三成</span></li>
<li>男性與女性當選率大致上以同曲線逐年上升(r=0.67)</li>
</ol><br />
<br />
<h3><span style="color: #6aa84f;">區域立委男女比例</span></h3><iframe frameborder="0" height="443.6541666666667" scrolling="no" seamless="" src="https://docs.google.com/spreadsheets/d/e/2PACX-1vQoQe7JWy1K4VUznvWdx1IYmjuPFKid8RK7lgZLx-pp0KPWLaB78gMcPJZ9kvDuxat-pcQQzjBXj1AT/pubchart?oid=969706671&format=interactive" width="717.5"></iframe><br />
在這張表中,我們可以看到<br />
<ol><li>女性參選比例從18.66%逐年提升到25.71%,增加7.05個百分點,提升幅度為37.78%</li>
<li>女性當選率比男性當選率高出2.5至8.57個百分點,以平均值來說,女性當選率只比男性高出4.7%</li>
<li>男性與女性當選率以近乎同曲線降低(r=0.97)</li>
</ol><br />
<br />
從上面兩張圖表分析近五次選舉後,我們可以得到三個結論<br />
<ol><li>女性參政比例逐年提升</li>
<li>當選率不分男女皆受當年環境較大影響</li>
<li><span style="color: yellow;">女性參選直轄市區域議員當選率高出男性三成</span></li>
</ol><br />
<br />
既然上面分析得出有個結果是女性參選議員較有優勢,那我們來看看區域立委與區域議員的女性參選率是不是也有差別。<br />
<a href="https://www.flickr.com/photos/doomdied/40111890814/in/dateposted-public/" title="snip_20180315174755"><img alt="snip_20180315174755" height="169" src="https://farm5.staticflickr.com/4783/40111890814_2a6f07263a_z.jpg" width="620" /></a><br />
將上述五筆選舉資料的女性比例做出比對,得出上圖,發現除了1998年的直轄市議員選舉外,其他後來的年份都高出區域立委20%以上,甚至高到54%的情況,但1998只有7%,數字差異很大,所以我又多找了一筆資料補上去(中選會資料庫的直轄市區域議員就只有六筆,沒辦法更多了),將資料補成這樣。<br />
<br />
<a href="https://www.flickr.com/photos/doomdied/39926108375/in/dateposted-public/" title="snip_20180315175841"><img alt="snip_20180315175841" height="150" src="https://farm1.staticflickr.com/811/39926108375_8a63259c75_z.jpg" width="640" /></a><br />
可以看到很有趣的分水嶺,到底1998~2002中間發生甚麼事,讓直轄市區域議員女性參選大增呢 ?<br />
<br />
查了一下之後,發現了<a href="http://weblaw.exam.gov.tw/LawArticle.aspx?LawID=A0025000&ShowType=MdfLaw">1999年一月25日公布的地方制度法</a>,第三十三條中這樣寫著<br />
<blockquote>各選舉區選出之直轄市議員、縣(市)議員、鄉(鎮、市)民代表<span style="color: red;">名額達四人者,應有婦女當選名額一人;超過四人者,每增加四人增一人</span>。直轄市選出之原住民名額在四人以上者,應有婦女當選名額;縣(市)選出之山地原住民、平地原住民名額在四人以上者,應有婦女當選名額;鄉(鎮、市)選出之平地原住民名額在四人以上者,應有婦女當選名額。</blockquote><br />
看起來感覺上就是這個四分之一的婦女保障名額,讓女性參選議員比例在2002年開始明顯多過於立委,然而這是個講究科學與證據的時代,雖然只有六筆資料不夠多,但將兩者數字去計算<a href="https://zh.wikipedia.org/wiki/%E7%9A%AE%E5%B0%94%E9%80%8A%E7%A7%AF%E7%9F%A9%E7%9B%B8%E5%85%B3%E7%B3%BB%E6%95%B0">相關係數</a>後得出 r = 0.75 ,在社會科學中這代表了兩者有高度相關性。<br />
<a href="https://www.flickr.com/photos/doomdied/39933487115/in/dateposted-public/" title="Capture"><img alt="Capture" height="94" src="https://farm1.staticflickr.com/812/39933487115_59c8dbd962_z.jpg" width="554" /></a><br />
很顯然的,1/4的婦女保障名額顯然讓直轄市區域議員女性參選比例高出許多,因為有保障名額後,整體而言女性當選機率比男性來的高,為了正確計算保障名額帶來的優勢,我重新算了最後四次立委與議員選舉的資料做為比對,去除了1999年之前還沒有1/4婦女保障名額的選舉。<br />
<br />
<br />
<a href="https://www.flickr.com/photos/doomdied/25957062057/in/dateposted-public/" title="Capture"><img alt="Capture" height="249" src="https://farm1.staticflickr.com/790/25957062057_30c19eabf6_c.jpg" width="800" /></a><br />
最終結果是這樣,以平均而言,<span style="color: red;">女性參選直轄市區域議員有高達六成</span>(61.71%)<span style="color: red;">的當選機率</span>,比男性的當選機率(46.85%)多了將近十五個百分點(14.86),也就是<span style="color: red;">相比男性參選者高了三成</span>(31.72%)<span style="color: red;">的機會當選</span>。而這樣的優勢,也讓女性參選直轄市區域議員的比例,比起沒有保障名額的區域立委多了三成(29.73%)。<br />
<br />
如果你直接拉到最後看結論的話,直接看下面這句就好。<br />
<blockquote><span style="color: yellow;">因為婦女保障名額,女性參選直轄市區域議員當選機率高達六成,相比男性參選者高了三成的機會當選,也讓女性參選直轄市區域議員的比例,比起沒有保障名額的區域立委多了三成</span>。</blockquote><br />
<br />
<h3><span style="color: #93c47d;">個人意見</span></h3>最後,我想說說我個人的看法,婦女保障名額在ROC體制的產生其實是ROC政府還在南京時候的事情,在當時的歷史環境確實沒有婦女保障名額,是很難有女性參政,而國民黨敗退到台灣後這情況也不會差很多,所以早期的時候我是認同婦女保障名額的,但是時代在變,法規也應該跟著時代進步,以平權的角度來看,近年在台灣參與選舉已經沒有性別上的門檻限制,台灣社會也早就沒了女性不應拋頭露面的觀念,客觀來說性別在參與選舉上是平等的。<br />
<br />
雖然早期就在推動兩性平權的呂秀蓮認為婦女保障名額應廢除,女性要的是平權而不是特權,婦女保障名額是對女性的歧視,也是對男性參選者的不公。但近期台灣的「女權主義」者卻不太這樣認為,不但有人認為要提高保障名額,還有所謂學者做出「受保障而當選的女性資歷不亞於她們擠下的男性候選人」這種狗屁不通的研究,然後一堆人藉此引用,跟本不懂這就是"garbage in garbage out"。選舉是民主制度,直接民選就是看選票說話,而不是比資歷,要比資歷的話大家報名資歷比一比就好了,何必投票 ? 何必選舉 ? 這種最根本的道理都不懂,還有臉寫出這種研究真是呵呵,靠性別當選而不是靠選票當選就是反民主也破壞了兩性平權,這種基本的認知很難懂嗎 ?<br />
<br />
先撇開台灣的女權是不是只想要特權,讓我們看看Liberté, Égalité, Fraternité(自由,平等,博愛)的來源-法國是怎麼做到兼顧民主、自由、平等的。<br />
<br />
其實很簡單,法國對於政黨提名上加了個<a href="http://trad.cn.rfi.fr/%E6%94%BF%E6%B2%BB/20170511-%E6%B3%95%E5%9C%8B%E7%AB%8B%E6%B3%95%E5%A4%A7%E9%81%B8%E6%8A%95%E7%A5%A8%E6%96%B9%E5%BC%8F%E5%8F%83%E9%81%B8%E7%95%B6%E9%81%B8%E6%A2%9D%E4%BB%B6">條件</a>,「<span style="color: #f3f3f3;">政黨提名的候選人性別差距超過百分之二,得到的政黨補助就會隨比例減少</span>」,這是一個非常聰明,一舉數得的做法,優點如下:<br />
<ol><li>規定提名的候選人而不是保障當選名額,能不能當選還是仰賴民主制度-選票。</li>
<li>對於政黨用補助去限制,有自由的選擇可以捨棄補助而提名更多某個性別,或是為了補助提名相同數量的候選人,且非政黨不受限制,確保了自由。</li>
<li>不會有因為性別而落選或當選的情況出現,保障了平等。</li>
</ol><br />
真的想要在參政上平權,真心建議廢除保障當選名額這制度,換一個更符合現今社會的方法吧,七十多年前的東西早就不適用於現今台灣社會了。<br />
<br />
如果要一句話形容婦女保障名額的話,大概是這樣<br />
<blockquote><b><span style="color: #f3f3f3;">婦女保障名額,一個破壞平等、反民主且帶著歧視的過時制度</span></b></blockquote><br />
<br />
<br />
Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com0tag:blogger.com,1999:blog-1829213234333389814.post-30291135616726392102018-01-30T11:08:00.000+08:002019-01-10T15:57:54.179+08:00[C#] OpenCvSharp DNN with YOLO2<a href="https://www.flickr.com/photos/doomdied/26090495898/in/dateposted-public/" title="yolo"><img alt="yolo" height="800" src="https://farm5.staticflickr.com/4761/26090495898_9ecb7ba29c_c.jpg" width="634"></a><br>
On my post "<a href="http://www.died.tw/2017/11/opencv-dnn-speed-compare-in-python-c-c.html">OpenCV DNN speed compare in Python, C#, C++</a>", Blaise Thunderbytes asked me to implement pjreddie's <a href="https://pjreddie.com/darknet/yolo/">YOLO</a> with OpenCvSharp, so that was why this post came out :P<br>
<br>
Since <a href="https://github.com/opencv/opencv/wiki/ChangeLog">OpenCV 3.3.1</a>, DNN moudle supported parse YOLO models, so we can easily using YOLO pre-trained model now. OpenCv Doc have a <a href="https://docs.opencv.org/3.4.0/da/d9d/tutorial_dnn_yolo.html">tutorial of YOLO object detection</a> writed in C++, if you using C++ can check it, I will using C# with OpenCVSharp.<br>
<br>
<a href="http://www.died.tw/2018/01/c-opencvsharp-dnn-with-yolo2.html#more">Read more »</a>Diedhttp://www.blogger.com/profile/17070040323517349661noreply@blogger.com18