یک نکتهی تکمیلی: UseHttpSys و استفادهی از HTTPS
netsh http add sslcert ipport=0.0.0.0:6001 certhash=<thumbprint of certificate> appid={new guid here} certstorename=Root
در اینجا در کنسول مدیریت مجوزهای ویندوز، به خواص آن مجوز مراجعه کرده و thumbprint آنرا در قسمت certhash وارد کنید. یک guid دلخواه را هم باید داخل {} در قسمت appid ذکر کنید. سپس این دستور را با دسترسی مدیریتی در خط فرمان ویندوز اجرا کنید. پس از آن است که برنامه برای مثال میتواند بر روی پورت 6001 که SSL تعریف شده، از http.sys استفاده کند.
مشکلات احتمالی
مجوزی که در اینجا ذکر میشود باید دارای private key بوده و همچنین در local computer وارد شده باشد؛ در غیراینصورت خطای زیر را دریافت میکنید:
SSL Certificate add failed, Error: 1312 A specified logon session does not exist.
It may already have been terminated.
- برای رفع آن از طریق اجرای certmgr.msc
، در قسمت «Personal/Certificates»، مجوز پیشفرض ASP.NET Core را با نام «ASP.NET Core HTTPS
development certificate»، انتخاب کرده و بر روی آن کلیک راست کنید. سپس گزینهی All tasks -> export را انتخاب کنید. نکتهی مهمی را که در اینجا باید رعایت کنید، انتخاب گزینهی «yes, export the private key» است. کپی و paste این مجوز از اینجا به جایی دیگر، این private key را export نمیکند. به همین جهت مشکل فوق را مشاهده خواهید کرد. در پایان این عملیات، یک فایل pfx را خواهید داشت. - اکنون برای import این فایل pfx، بجای ورود به قسمت مدیریت مجوزهای ویندوز از طریق certmgr.msc، نیاز است از طریق mmc و add remove snap-ins وارد شوید تا بتوانید به local computer دسترسی پیدا کنید.
- به همین جهت در خط فرمان دستور mmc را وارد کرده و آنرا اجرا کنید. در صفحهی باز شده، گزینهی File -> Add remove snap-ins را انتخاب کنید. در اینجا در لیست ظاهر شده، certificates را انتخاب و add کنید. در صفحهی بعدی نیز computer account و سپس local computer را انتخاب کنید. اکنون بر روی دکمهی ok کلیک کنید تا صفحهی certificate مخصوص local computer ظاهر شود. حالت استفادهی از certmgr.msc، مجوزهای current user را نمایش میدهد که برای اینکار مناسب نیست.
- در پایان، این فایل pfx را در مسیر «Trusted Root Certification Authorities/Certificates» قرار دهید. برای اینکار بر روی نود certificate کلیک راست کرده و گزینهی All tasks -> import را انتخاب کنید. سپس مسیر فایل pfx خود را داده و این مجوز را import نمائید.
- اکنون اگر مجددا دستور netsh http add sslcert یاد شده را اجرا کنید، بدون مشکل اجرا میشود.