【发布时间】:2017-08-16 01:30:16
【问题描述】:
我正在使用 RESTful 服务,发现 Postman 是 GET、POST 和测试 API 的最佳插件之一。
我在 postman 中找到了 Basic Auth、No Auth、DIgest Auth、OAuth、AWS。如何测试授权控制器和方法。
我知道 Authorize 属性检查 user.Identity.IsAuthenticated
我不确定如何使用 Postman 在控制器和具有特定角色的方法中传递授权,如下所示
[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);
}
【问题讨论】:
-
web api 使用什么类型的身份验证提供程序?
-
我认为默认身份验证是基本的。我没有对 Web Api 身份验证进行任何更改
-
请分享Startup.Auth.cs或者authprovider的配置所在的启动类
-
@MarcusH 我发布了我的 startup.auth.cs
-
@chatra,您似乎正在使用 Windows 身份提供程序并使用 OAuth 2.0。而且您也不会在使用邮递员时发送角色。授权由框架根据用户声明处理。如果我误解了你的问题,请告诉我
标签: c# asp.net-web-api postman asp.net-web-api2