c# - 在 ServiceStack 中注册自定义凭据身份验证提供程序

标签 c# servicestack authorize-attribute

我正在读这个from their documentation其中说:

Then you need to register your custom credentials auth provider:

  //Register all Authentication methods you want to enable for this web app.
  Plugins.Add(new AuthFeature(() => new AuthUserSession(),
      new IAuthProvider[] {
    new CustomCredentialsAuthProvider(), //HTML Form post of UserName/Password credentials
      }
  ));

我的问题是:我应该把它放在哪里?另外,评论“用户名/密码凭据的 HTML 表单帖子”是什么意思?

目前,我有一个 ServiceStack 服务,它在调用时返回 JSON。我想在它上面添加一个 Authorize 属性,这样只有授权用户才能访问它。

我按照他们的建议创建了一个类:

public class CustomCredentialsAuthProvider : CredentialsAuthProvider
{
    public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
    {
        //Add here your custom auth logic (database calls etc)
        //Return true if credentials are valid, otherwise false
        return false;
    }

    public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
    {
        //Fill the IAuthSession with data which you want to retrieve in the app eg:
        session.FirstName = "some_firstname_from_db";
        //...

        //Important: You need to save the session!
        authService.SaveSession(session, SessionExpiry);
    }
}

我如何“注册您的自定义凭证授权提供商?”

最佳答案

您在 AppHostConfigure 方法中注册您的插件。该评论仅在 example 中使用您从中提取代码以表明 CustomCredentialsAuthProvider 可以与表单中的 HTTP POST 一起使用。

public class MyApphost : AppHostHttpListenerBase
{
    public MyApphost() : base("Service Name", typeof(MyApphost).Assembly) {}

    public override void Configure(Container container)
    {
        Plugins.Add(new AuthFeature(
            () => new AuthUserSession(),
            new IAuthProvider[] { new CustomCredentialsAuthProvider()}
        ));
    }
}

关于c# - 在 ServiceStack 中注册自定义凭据身份验证提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18921336/

相关文章:

c# - 实体管理器 : 3 Tier/Layer Application Question

asp.net - 使用 Html.Action 调用 [ChildActionOnly] 操作方法时的安全问题

asp.net-mvc-5 - MVC5 身份验证 : Authorize attribute on every controller or base controller

c# - 在不调试的情况下将测试日志写入 Visual Studio 输出 Pane ?

c# - 使用私有(private)还是使用属性? C#

servicestack - 是否有一个使用新 API 的相当完整的示例项目服务堆栈?

servicestack - ServiceStack 中的缓存(ETags 和 If-None-Match)

asp.net-mvc-3 - MVC 配置授权角色值和强类型角色

c# - morelinq 通过多个属性来区分

dynamic - 在 C# 中获取动态类型的 ServiceStack.redis 客户端