Microsoft Applied Skills: Develop an ASP.NET Core web app that consumes an API

Recap สักนิดก่อน Microsoft Applied Skills เป็นตัว Cert เล็ก (ฝั่ง Microsoft เค้าจัดคำใหม่ว่า Credentials) ที่เราสามารถวัดทักษะที่สนใจเท่านั้น เช่น ทำงาน Azure Container Apps อย่างเดียว ก็เรียน และวัดทักษะ (Assessment) ในส่วน Azure Container Apps เท่านั้น ไม่ต้องมาสนใจ Azure VM / Azure App Service แบบตัว Cert เต็มอย่างเจ้า AZ-104 ครับ

ตอนแรกไม่ได้ตั้งใจจะเขียน Blog นะ แต่แบบว่าเราลองทำแล้ว Code + Deploy App Service มันน่าจะผ่านนี่ Test ผ่าน ทำถูกหมดแล้วนะ แล้วมีคนบ่นใน Incorrect assessment result for Develop an ASP.NET Core web app that consumes an API - Microsoft Q&A เลยลองมาเขียน Blog ดีกว่า รอบแรกๆ ผม Fail เหมือนกัน จากหมวดที่ประเมิน จะเห็นมันเขียนไม่ค่อย Clear ผมเลยลอง Note ๆ ไว้ดีกว่า

สรุปแล้ว safe HTTP / unsafe HTTP คือ อะไร ?

Ensure that the 'safe' HTTP operations, such as GET, HEAD, OPTIONS, TRACE cannot be used to alter any server-side state.

Ensure that any 'unsafe' HTTP operations, such as POST, PUT, PATCH and DELETE, always require a valid CSRF token + Authentication + Authorization

อันนี้ยอมรับเลย น่าจะเคยเรียน แล้วลืมไปแล้ว สรุป

  • 'safe' HTTP operations พวก GET, HEAD, OPTIONS, TRACE มันต้องแก้ข้อมูล server-side ไม่ได้
  • 'unsafe' HTTP operations พวก POST, PUT, PATCH and DELETE แก้ได้ ต้องเป็นคนที่ใช่ ตัวจริงมีสิทธิ

หลักๆใช้ HTTP operations ให้ถูกกับงานครับ / Authentication + Authorization หรือ เพิ่มเติมไป มีการป้องการโจมตีแบบ CSRF token (แก้โดยพวก 'unsafe' HTTP operations มันจะมี token สุ่มไปกับ form - NET8 จะมี middleware มาให้เลยนะ)

อ่อ มันใช้ JSON Post ตรงๆไม่ได้ด้วยนะ

  • แบบแรก - ไม่รอด แต่ทำงานได้เหมือนกันนะ 555
httpClient.PostAsJsonAsync
  • ลองใช้ StringContent Class ผ่าน ตอนนี้เข้าใจว่าต้องบอก httpClient ว่าจะส่งอะไรเข้าไป มันจะได้ HttpResponseMessage ออกมาถูกต้อง เคสนี้้จะเป็น "application/json"
    Ref: Make HTTP requests with the HttpClient - .NET | Microsoft Learn
// Serialize the information to be added to the database
var jsonContent = new StringContent(JsonConvert.Serialize(YourModel),Encoding.UTF8,"application/json");

// Create the HTTP client using the FruitAPI named factory
var httpClient = _httpClientFactory.CreateClient("MyClient");

// Send Post 
using HttpResponseMessage response = await httpClient.PostAsync("api/your_target_api", jsonContent);

ทำพวก Authentication + Authorization น่าจะใช้แต่ใส่ Api Key

builder.Services.AddHttpClient("FruitAPI", httpClient =>
{
    httpClient.BaseAddress = new Uri("<<--YOUR_END_POINTS-->>");
    httpClient.DefaultRequestHeaders.Clear();
    httpClient.DefaultRequestHeaders.Add("X-API-KEY", "myApiKey"));
});

CSRF token มีใส่ไปนะ แต่จริงโจทย์ให้เราทำ Razor / NET7 (Ref: Prevent Cross-Site Request Forgery (XSRF/CSRF) attacks in ASP.NET Core | Microsoft Learn) เลยไม่แน่ใจว่าจำเป็นไปนะ ไม่แน่ใจว่า Code Backends ทำไว้ไหม ว่าจะลองดึงลงมาแงะ แต่ไม่ทัน 55555

สรุปลองอีกรอบที่ 3 ผ่าน แต่ไม่ได้เต็มนะ เลยมาจดไว้ใน Blog ก่อน สำหรับใครอยากรู้ว่า Microsoft Applied Skills ลองไปดู Blog นี้ได้ครับ ลอง Microsoft Applied Skills Credentials รอบนี้ Coding บน VSCode ผ่าน VDI หน่วงระดับนึงเลย แล้วรู้สึกได้เลยว่าขาด Co-Pliot แล้ว มันเหมือนขาดอะไรเลยติดกด tab 5555

ถ้ารอบแรก Fail จะต้องรอ 72 ชั่วโมง แล้วปุ่ม Retake จะขึ้นมาอีกรอบครับ

ถ้าใครอยากมาลองทดสอบ Applied Skills ASP.NET ตามมาได้ครับ Develop an ASP.NET Core web app that consumes an API - Training | Microsoft Learn
- Coding เล็กน้อย + Azure App Service ครับ

Reference


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts to your email.