我必須將我的 Web 應用程式配置為使用 ServiceStack 內置的 ApiKeyAuthProvider。我已經在容器中注冊了帶有 IAuthRepository 介面的 OrmLiteAuthRepository,但是它拋出了一個例外,說我沒有注冊 IUserAuthRepository。有人可以解釋我的區別嗎?提前致謝
編輯: 對不起,我混淆了錯誤是
System.NotSupportedException: 'ApiKeyAuthProvider requires a registered IAuthRepository'
我們 AppHost 的 Configure 方法是
public override void Configure(Container container)
{
var dbFactory = new OrmLiteConnectionFactory("connString", SqlServerDialect.Provider);
container.Register<IDbConnectionFactory>(dbFactory);
container.Register<IUserAuthRepository>(_ => new OrmLiteAuthRepository(dbFactory));
container.Resolve<IUserAuthRepository>().InitSchema();
var authProvider = new ApiKeyAuthProvider()
{
RequireSecureConnection = false
};
Plugins.Add(new AuthFeature(
() => new AuthUserSession(),
new IAuthProvider[] {
authProvider
}
));
}
你能解釋一下這兩個介面之間的區別嗎?我們無法弄清楚(ServiceStack v.6.0.2)
uj5u.com熱心網友回復:
有關正確用法的示例,請參閱Auth Repository 檔案,例如:
container.Register<IDbConnectionFactory>(c =>
new OrmLiteConnectionFactory(connectionString, SqlServer2012Dialect.Provider));
container.Register<IAuthRepository>(c =>
new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>()));
container.Resolve<IAuthRepository>().InitSchema();
這IAuthRepository是所有 Auth Repositories 必須實作的最小介面,同時IUserAuthRepository是擴展介面,用于啟用擴展功能以啟用所有 ServiceStack 內置 Auth Repositories 也實作的附加功能。但是您永遠不需要注冊或決議 a IUserAuthRepository,即它們應該只在主IAuthRepository介面上注冊。
決議 Auth 存盤庫
如果您需要,可以從您的 Service 中base.AuthRepository或base.AuthRepositoryAsync在您的 Service 中訪問 Auth Repository,您可以在其中使用任何IUserAuthRepositoryAPI,因為它們都可以作為擴展方法在 上使用IAuthRepository,例如此示例 Service 呼叫該IUserAuthRepository.GetUserAuth()方法:
public class MyServices : Service
{
public object Get(MyRequest request) =>
AuthRepository.GetUserAuth(request.UserId);
}
雖然這里是在您的服務之外訪問 Auth 存盤庫的推薦 API:
var authRepo = HostContext.AppHost.GetAuthRepository();
var authRepoAsync = HostContext.AppHost.GetAuthRepositoryAsync();
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/426926.html
