c# - 在 Web Api 中使用 Postman 授权属性认证

标签 c# asp.net-web-api postman asp.net-web-api2

我正在使用 RESTful 服务,发现 Postman 是 GET、POST 和测试 API 的最佳插件之一。

我在 postman 中找到了 Basic Auth、No Auth、DIgest Auth、OAuth、AWS。如何测试授权 Controller 和方法。

我知道 Authorize 属性检查 user.Identity.IsAuthenticated

我不确定如何使用 Postman 在 Controller 和具有特定角色的方法中传递授权,如下所示

[Authorize(Roles = "Admin, Super User")]

public ActionResult AdministratorsOnly()
{
    return View();
}

这是我的启动文件

  public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }

    public static string PublicClientId { get; private set; }

    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context and user manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

        // 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
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Configure the application for OAuth based flow
        PublicClientId = "self";
        OAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider(PublicClientId),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
            // In production mode set AllowInsecureHttp = false
            AllowInsecureHttp = true
        };

        // Enable the application to use bearer tokens to authenticate users
        app.UseOAuthBearerTokens(OAuthOptions);         
    }

最佳答案

1.在 web api 中启用 CORS

在 Startup.cs 配置方法中将以下内容附加到 IAppBuilder(如果遇到问题,请在此处阅读更多信息 How to make CORS Authentication in WebAPI 2?)

app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

Nuget 包 here

<强>2。通过 Postman 获取 token

enter image description here

3.使用 token 并从 web api 获取数据

注意: token 响应包含作为 token 的 access_token 和作为承载的 token 类型。在请求中使用时,在 Authorization http header 的值之间添加一个空格。身份验证服务器将解析 token 并在请求命中所请求 Controller 中的 [Authorize] 属性之前设置 user.Identity

enter image description here

此外,请确保 ApplicationOAuthProvider 将包含当前角色的声明身份添加到 token 中。否则请求将被拒绝。测试它的一种方法是只使用没有角色的 [Authorize] 属性,然后查看 postman 是否可以访问 Controller

关于c# - 在 Web Api 中使用 Postman 授权属性认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42976982/

相关文章:

java - Apache HttpClient postman

java - 如何将请求正文中的列表值发送到 Rest Api

c# - Monodroid 环境变量

c# - 无法安装 System.Data.SqlClient

c# - 使用依赖注入(inject)统一配置的简单授权服务提供者

javascript - 无法使用 Postman 查询数据

c# - 将 XElement 添加到 XDocument 时出现重复元素

c# - 查询 DocumentDB 中的分区列表(和/或分区键值)

c# - Windows 登录的使用条件对话框

c# - 有没有办法用 WebAPI 生成 Urls?