c# - 使用 OAuthAuthorizationServer 自定义允许的授权类型

标签 c# oauth-2.0 asp.net-web-api2

我正在为 Web api 实现 OAuth 2.0。最初,我想允许的唯一授权类型是资源所有者密码授权类型的“密码”。将来,我可能会扩展到其他股票授予类型,甚至构建自定义类型。为了实现,我在我的 Startup.cs 类中创建了以下代码。我没有指定授权端点,只是一个 token 端点。

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

    public void ConfigureAuth(IAppBuilder app)
    {

        var myOAuthServerProvider = new MyOAuthServerProvider();

        app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
        {

            // mark true if you are not on https channel. This should never be true for Production.
            AllowInsecureHttp = true,

            //Enable a 60 minute expiration time.
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60),

            // Allows the authorization server to alter the response coming out so it can report a 401.
            AuthenticationMode = AuthenticationMode.Active,

            // Provider needs to be the custom class that performs our authentication. 
            Provider = myOAuthServerProvider,

            // This specifies the endpoint path where you can generate a token. 
            TokenEndpointPath = new PathString("/api/token"),

        });
    }
}

对于 MyOAuthServerProvider 类,我应该从 OAuthAuthorizationServerProvider 继承并覆盖特定方法以仅允许我想要启用的授权类型,还是应该从头开始从 IOAuthAuthorizationServerProvider 接口(interface)实现 MyOAuthServerProvider?

最佳答案

要只允许您想要的授权类型,从 OAuthAuthorizationServerProvider 继承就足够了。 .然后你需要重写两个方法:

  • 验证客户端身份验证 - 验证请求的来源是注册的 client_id
  • GrantResourceOwnerCredentials - 验证提供的 usernamepasswordgrant_type设置为 password

  • 有关更多信息,请参阅 的文档。 GrantResourceOwnerCredentials 方法:

    Called when a request to the Token endpoint arrives with a "grant_type" of "password". This occurs when the user has provided name and password credentials directly into the client application's user interface, and the client application is using those to acquire an "access_token" and optional "refresh_token". If the web application supports the resource owner credentials grant type it must validate the context.Username and context.Password as appropriate. To issue an access token the context.Validated must be called with a new ticket containing the claims about the resource owner which should be associated with the access token. The application should take appropriate measures to ensure that the endpoint isn’t abused by malicious callers. The default behavior is to reject this grant type.

    关于c# - 使用 OAuthAuthorizationServer 自定义允许的授权类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32616069/

    相关文章:

    c# - .NET 6 Web API 中的 AddSerilog 和 UseSerilog 有什么区别?

    c# - 在 C# 中重命名 Excel 工作表名称

    authentication - 是否可以将 "get token"和 "get userinfo"步骤合二为一?

    c# - 如何通过 webAPI 传递\用户 azure continue token

    c# - 让 WSHttpBinding 通过 http 代理工作

    c# - ConfigurationManager.ConnectionStrings.ConnectionString 问题

    javascript - hello.js:是否可以动态设置提供程序的设置?

    oauth-2.0 - Azure AD B2C : User. Identity.Name 为 null,但 User.Identity.m_instance_claims[9] 具有名称

    c# - 将 Web API 2 与 Kentico 结合使用时,SiteContext 为 null

    c# - 使用 distance() 方法进行空间 Linq 查询