2016-08-22 107 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 –