我正在读这个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);
}
}
我如何“注册您的自定义凭证授权提供商?”
最佳答案
您在 AppHost
的 Configure
方法中注册您的插件。该评论仅在 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/