c# - ASP.NET Identity Token Methods 接受所有 HTTP 方法

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

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

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

这是预期的吗?我假设它应该返回不支持的方法而不是 POST 请求。

最佳答案

您可以编写自定义 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);

        }
    }

关于c# - ASP.NET Identity Token Methods 接受所有 HTTP 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47258538/

相关文章:

c# - 如何在 ASP.Net 中更改 UnauthorizedAccessException 重定向到的位置

c# - 在 asp.net WebAPI 中返回数组对象

asp.net-mvc - 从 ASP.NET Identity 2.x 中的角色中删除用户

indexing - ArangoDb 上的数组索引

c# - 我可以在不断开与服务器的连接的情况下重用 HttpWebRequest 吗?

c# - 在 ASP.net 中创建嵌入式图像按钮?

c# - 为什么 DateTime.Now 和 StopWatch 会漂移?

asp.net-mvc - 没有模型的 WebApi GET 和多个 POST 参数始终为 null

iis - 在 web.config 中添加 x-frame-Options 返回错误 500

c# - 密码登录异步。参数空异常 : Value cannot be null