【问题标题】:ASP.NET Identity Token Methods accepts all HTTP methodsASP.NET 身份令牌方法接受所有 HTTP 方法
【发布时间】:2018-04-25 19:02:32
【问题描述】:

我创建了 pone webapi 并实现了身份验证。我有令牌方法来获取用户令牌。一切正常。

场景: 我用邮递员测试了令牌方法。这里我注意到我可以使用任何类型的 HTTP 方法来请求令牌。我认为 /token 方法应该只支持 POST 方法。但是当我使用 DELETE 方法时,我也得到了令牌。同样,我也可以使用 PUT、PATH 等。

这是预期的吗?我假设它应该返回除 POST 请求之外的 Method Not Supported。

【问题讨论】:

  • 我也面临同样的问题,但它是卷曲的。你能检查一下 curl 中的方法并告诉我输出吗?请检查链接,stackoverflow.com/questions/47049550/…
  • 您可以编写您的自定义 OAuthAuthorizationServerOptions.Provider。并使用上下文仅接受 Http Post 请求。
  • @kvk30,正如你所指定的,我也尝试了 curl 并遇到了同样的问题
  • @Saadi 您能告诉库名称以启用这些选项吗?
  • 您是否愿意,我可以发布答案以便其他用户可以看到?

标签: c# asp.net-web-api asp.net-identity


【解决方案1】:

您可以编写您的自定义 OAuthAuthorizationServerOptions.Provider。并使用上下文仅接受 Http Post 请求

OAuthAuthorizationServerOptions 是 asp.net 身份核心类。您可以在此命名空间 Microsoft.Owin.Security.OAuth 下找到它。

示例代码:

public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }

        public void ConfigureOAuth(IAppBuilder app)
        {
            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());
        }
    }

    public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
    {
        public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
            context.Validated();
        }

        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            if (context.Request.Method != "POST")
            {
                context.SetError("invalid_request", "Invalid request");
                return;
            }

            using (AuthRepository _repo = new AuthRepository())
            {
                IdentityUser user = await _repo.FindUser(context.UserName, context.Password);

                if (user == null)
                {
                    context.SetError("invalid_grant", "The user name or password is incorrect.");
                    return;
                }
            }

            var identity = new ClaimsIdentity(context.Options.AuthenticationType);
            identity.AddClaim(new Claim("sub", context.UserName));
            identity.AddClaim(new Claim("role", "user"));

            context.Validated(identity);

        }
    }

【讨论】:

  • 请也写下那个方法名(request.method()),这样更清楚。
  • You can write your custom OAuthAuthorizationServerOptions.Provider 怎么样?
  • 如果它对您有用,您能否将此答案设为有用(通过单击向上箭头)?这对其他人和我都会非常有帮助。
猜你喜欢
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多