我正在使用 RESTful 服务,并发现 Postman 是 GET、POST 和测试 API 的最佳插件之一。
我在邮递员中找到基本身份验证、无身份验证、DIgest Auth、OAuth、AWS。如何测试授权控制器和方法。
我知道授权属性检查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);
}
1. 在Web api中启用CORS
将以下内容附加到 Startup.cs 配置方法中的 IAppBuilder(如果您遇到问题,请在此处阅读更多信息如何在WebAPI 2中进行CORS身份验证? https://stackoverflow.com/questions/20079813/how-to-make-cors-authentication-in-webapi-2)
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
努吉特包here https://www.nuget.org/packages/Microsoft.Owin.Cors/
2.通过Postman获取token
3.使用token并从web api获取数据
注意:令牌响应包含 token 的 access_token 和 bearer 的 token_type。在请求中使用时,请在授权 http 标头的值之间添加空格。 auth 服务器将在请求到达请求的控制器中的 [Authorize] 属性之前解析令牌并设置 user.Identity
另外,请确保 ApplicationOAuthProvider 将包含当前角色的 ClaimIdentity 添加到令牌中。否则请求将被拒绝。测试它的一种方法是仅使用不带角色的 [Authorize] 属性,然后查看邮递员是否可以访问控制器
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)