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

时间:2013-09-20 16:15:50

标签: c# servicestack authorize-attribute

我正在阅读from their documentation,其中说:

  

然后您需要注册自定义凭据身份验证提供程序:

  //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);
    }
}

如何“注册自定义凭据身份验证提供程序?”

1 个答案:

答案 0 :(得分:4)

您可以在Configure的{​​{1}}方法中注册插件。该评论仅用于您提取代码的example,以表明AppHost可以使用表单中的HTTP POST。

CustomCredentialsAuthProvider
相关问题