c# - 仅对某些 Controller 使用 OAuth Bearer token

标签 c# asp.net oauth

使用 OAuth 允许访问网络 API。我有这样的东西(例如来自各种教程:http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/):

public void ConfigureOAuth(IAppBuilder app, System.Web.Mvc.IDependencyResolver kernel)
{
    OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
    {
        AllowInsecureHttp = true,
        TokenEndpointPath = new PathString("/token"),
        AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
        Provider = new SimpleAuthorizationServerProvider(),
    };

    // Token Generation
    app.UseOAuthAuthorizationServer(OAuthServerOptions);
    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

}

Startup.cs 中。但这会为我所有的 Controller 启用全局授权。有没有办法只为单个 Controller (或少数 Controller 或方法)打开它?我对网站的其他部分有自定义授权,我可以进入它们并检查传递的凭据是否是不记名 token 并拒绝它,但我想知道是否没有更优雅的解决方案。

最佳答案

我最近一直在玩 Oauth,遇到了一个不同的场景,我需要对 API 中的某些 Controller [allowAnonymous],同时拒绝对项目的其余部分进行匿名。

我不知道这是否对您的情况有帮助,但我想我会分享以防万一它有帮助。

将以下内容添加到我需要允许匿名的每个 Controller 的 API 的 Web.config 中,以解决我的场景。

</system.codedom>
  <location path="api/controller">
    <system.web>
      <authorization>
        <allow users="?"/>
      </authorization>
    </system.web>
  </location>
</configuration>

关于c# - 仅对某些 Controller 使用 OAuth Bearer token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47083475/

相关文章:

c# - 总是遍历业务层到达数据层?

jquery - 显示时从元素获取文本值 :none

使用 AWS Cognito、Google+ OAuth 和 AccountManager 的 Android 应用程序

angular - 使用 Angular 向 facebook oauth 发送 CORS 问题

c# - 数据绑定(bind) : One vs two way

c# - MongoDB 引用的最佳实践

c# - 指定的转换无效 - double 列表到 float 列表

asp.net - asp :ButtonField click 之前的 Javascript

ASP.NET - 构建您自己的路由系统

java - 在 Android 应用程序中将 Web 应用程序的 oauth 请求结果 Hook 到 Twitter