2016-08-22 47 views
3

我已經創建了一個Web API,並將應用授權屬性應用於API控制器。現在,我想用POSTMAN測試它,但是我收到了授權錯誤。asp.net weapi授權郵遞員

控制器代碼:

[Authorize] 
     [HttpPost] 
     public IHttpActionResult Attend([FromBody] int gigId) 
     { 

      var attendance = new Attdendance 
      { 
       GigId = gigId, 
       AttendeeId = User.Identity.GetUserId() 
      }; 

      _context.Attdendances.Add(attendance); 
      _context.SaveChanges(); 
      return Ok(); 
     } 

我的要求是這樣的http://prntscr.com/c8wz0b

我用這提前郵遞員其餘客戶http://prntscr.com/c8xafd

如何通過authroization在崗的人嗎?

回答

7

編輯23/08/2016 我想你是在cookie認證與身份

// Enable the application to use a cookie to store information for the signed in user 
      // and to use a cookie to temporarily store information about a user logging in with a third party login provider 
      // Configure the sign in cookie 
      app.UseCookieAuthentication(new CookieAuthenticationOptions 
      { 
       AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
       LoginPath = new PathString("/Account/Login"), 
       Provider = new CookieAuthenticationProvider 
       { 
        // Enables the application to validate the security stamp when the user logs in. 
        // This is a security feature which is used when you change a password or add an external login to your account. 
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
         validateInterval: TimeSpan.FromMinutes(30), 
         regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) 
       } 
      });  

這是Visual Studio中具有標識的默認配置。 我可以爭論爲什麼它不是安全的好選擇,但那不是重點。

你可以去白衣它的「郵遞員」,但它的棘手 這是我要做的事:

  1. 請在您的登錄頁面的請求: enter image description here
  2. 獲取的反僞造令牌形式: enter image description here
  3. 請登錄頁面上POST請求與以數據的形式這個post數據: enter image description here

現在你的郵遞員得到驗證cookie,你可以請求Web API與[授權]標籤

編輯

對於工具,你必須添加一個授權頭。

  • 圍棋的頭形式
  • 添加的HTTP標頭 「授權」
  • 單擊編輯按鈕等瞧;)

screen shot

以前的答案被刪除

+0

這是用於基本身份驗證,取決於您的身份驗證機制 – Mathieu

+0

我正在使用身份。我用上面的方法,但仍然得到認證錯誤,像這樣http://prntscr.com/c908zb –

+0

我添加一個方法去cookie認證。每次認證會話關閉時,您都必須這樣做。我建議你切換到基於令牌的身份驗證,如果你必須從例如電話應用程序使用你的網絡API,例如。維護電話應用程序的cookie是一團糟;) – Mathieu

2

對於郵差Windows應用程序4.6.0:

  1. 從您的請求收集
  2. 去選擇你的要求,以「授權」標籤
  3. 選擇適當的「類型」,如「基本身份驗證」
  4. 輸入 「用戶名」 和 「密碼」
  5. 點擊 「更新請求」
+0

對不起我使用其他職位的人(https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo) –

+0

我沒有看到授權選項卡http://prntscr.com/c8xoiy –

+0

甚至沒有設置。 http://prntscr.com/c8xorb我使用高級郵差休息客戶端不郵遞員Windows應用程序4.6.0 –