function เข้ารหัส SHA1

Public Shared Function hashPassword(ByVal password As String) As String
        If password = String.Empty Then
            Return String.Empty
        Else
            Dim SHA1KeyedHasher As SHA1 = New SHA1Managed
            Dim encode As New UTF8Encoding
            Return encode.GetString(SHA1KeyedHasher.ComputeHash(encode.GetBytes(password)))
        End If
    End Function

upload image กับ asp.net

เช้าวันนี้เดินหาหอพัก ตั้งแต่ ซอย 2  ไปถึง ซอย 13 กว่าจะได้ที่ซุกหัวนอน เออ  มีเน็ตด้วย แต่ไม่รู้จะเร็วป่าวนะ กลัวมันโหลดอะไรกันจังเลยนิ อย่างน้อยๆ ก็มีที่พักกันยาวล่ะคราวนี้ เดือนนี้มาปักหลักอยู่แถวๆวิลัยนี้แหละ มาอุ่นโน้ตบุ้คคู่ใจ ด้วย dota ไป 1 รอบ เจอ step noob เข้าไป lose คับพี่น้อง หลังจากนั้นก็ได้เวลา ลุยงานซะที

วันนี้บุกโจมตี FileUpload ของ asp.net คับ Hahahahaha เล่นไม่ยากนะ มีไม่กี่อย่างเอง

ตรวจเช็คว่ามีไพล์อับมาไหม ก็ใช้ FileUpload.HasFile  ได้ค่า Return ออกมาเป็น Boolean

หาประเภทของไพล์ก็ FileUpload.PostedFile.ContentType ก็จะได้ออกมาประมาณว่า
image/pjpeg,   applicaton/x-shockwave-flash อะไรก็ว่ากันไป

ขนาดของไพล์ที่อับโหลดมาก็ FileUpload.PostedFile.ContentLength  จะได้ออกมาเป็น bytes นะคับ

อ้อ ชื่อไพล์ก็จะเป็น FileUpload.FileName ก็จะได้ชือไพล์ที่อับโหลดเข้ามา

ตอน Save ไพล์ที่อับโหลดมาก็ใช้ตัวนี้

FileUpload.PostedFile.SaveAs(path_file) ตรง path_file ก็ไปเลือก ๆ ลงเอากันเอง อ่ะ….ให้ดูตัวอย่างก็ได้ ของผมก็จะประมาณนี้คับ 

Dim path_file As String = Server.MapPath(“”) & “\uploaded\” & FileUpload.FileName

อันนี้เอาไว้ ลบไพล์ที่เรา Upload มาไว้แล้ว    System.IO.File.Delete(path_file)   ตรง paht_file ก็ไปเปลี่ยนกันซะว่าจะเป็น ตำแหน่งของไพล์ว่ามันอยู่ไหน

Path ของไพล์ก็ต้องนี้เลย   Server.MapPath(“”) มันจะได้ค่าของ dir ที่ทำงานอยู่ หมายถึง  d:\Project\  อะไรแบบเนี้ยแหละ แล้วแต่ folder ใคร folder มัน

ความจริงผมเขียน upload รูปภาพ แล้วก็ resize รูป อ่ะคับ ได้ error มาตัวหนึ่ง  เอามาฝากไว้ล่ะกัน เผื่อใครเจอ เอาไว้แก้ไขนะคับ ให้แก้ Permission ของ Folder ที่ไพล์นั้นมันอยู่ นะคับ แค่เนี้ยแหละ bug กระเด็นเลย ไม่ต้องถึงกับกระโดนเท้าคู่ แค่สะกิต ก็หลุดแล้ว เอิ๊ก ๆๆๆ

A generic error occurred in GDI+.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

Server Error in '/' Application. ทำไงดี ?

Server Error in ‘/’ Application.


Runtime Error

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a “web.config” configuration file located in the root directory of the current web application. This <customErrors> tag should then have its “mode” attribute set to “Off”.

<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>


Notes: The current error page you are seeing can be replaced by a custom error page by modifying the “defaultRedirect” attribute of the application’s <customErrors> configuration tag to point to a custom error page URL.

<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
    </system.web>
</configuration>

วิธีแก้ปัญหานะคับ

1. ที่มาของปัญหานี้
(ปัญหานี้จะเกิดขึ้นตอนอยู่บน server จริงเท่านั้น ในตอนที่เรากำลัง พัฒนาโปรแกรม ทดสอบโปรแกรมอยู่บน localhost มันจะไม่แสดงของความผิดพลาดแบบนี้ )

เรื่องของเรื่องคือว่า เราเขียนโปรแกรม asp.net แล้วโค้ดมันเกิด error ขึ้นมา แล้วระบบ มันก็แสดงข้อความบอกให้เรารู้ว่า มัน error นะ แต่ว่ามันไม่ยอมบอกว่า error ตรงไหน

รู้ป่าว ว่าทำไมระบบ .net ของ MS นั้นมันไม่ยอมบอกว่า error ตรงไหน เพราะว่ามันกำลังปิดบังความลับของเราไว้นั้นเอง ลองนึกดูนะ ถ้าระบบมัน error แล้วมันก็ต้องบอก โค้ดส่วนที่มัน error แล้วก็จะบอกถึงรายละเอียดที่มัน error ด้วย ถ้าเป็นแบบนั้นจริง คนที่มา hack เว็บเราไม่ hack ง่ายเลยเหรอ  ผมเชื่อว่า MS เค้าเรียนรู้ความเจ็บปวดจาก asp มามากพอแล้ว

มันมีให้เราเลือกด้วย ว่าตอนโปรแกรม error จะให้ทำอะไรดี ระหว่าง

จัดการข้อผิดพลาดด้วยตัวเราเอง แบบว่า ปิดโหมด customer error ซะ ก็ให้มันแสดงส่วนที่ผิดพลาดออกมาเลย ก็จะเป็นแบบนี้คับ เอาโค้ดนี้ใส่ text edit แล้ว save เป็นชื่อ web.config แล้วอับขึ้น server วางไว้ root folder เลยนะคับ

web.config.custom_error_off

หรือ

เกิดการผิดพลาดอะไรขึ้นมา ให้กระโดดไปหน้า ที่เราต้องการเลย แบบนี้คับ สร้างไพล์ web.config แล้วก็อับขึ้น server ไปเลย อ้อ..ตรง default redirect ที่เห็นเป็น

mycustompage.htm ตรงนี้ไพล์นี้จะถูกเรียกใช้งานก็ตอนมันเกิด error คับ

web.config.custom_error_dedirect

แต่ส่วนใหญ่ผมจะเขียนโค้ด try….catch ดักมันไว้ก่อนแล้วน่ะคับ

Get Adobe Flash playerPlugin by wpburn.com wordpress themes