一部の呼び出しで認証が必要で、一部の呼び出しで認証が不要な REST サービス要件があります。呼び出しはすべて互いに独立しているため、状態はまったく使用されません。うまくいくように見えるものをまとめましたが、これはセッションを使用しない正しい方法ですか?
この質問は、ここで回答されている私の WCF の質問に関連しています。
まず、認証方法を登録しました。
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[] {
new CustomCredentialsAuthProvider(), //HTML Form post of UserName/Password credentials
}
));
次に、それぞれの呼び出し (またはサービスまたは DTO) に Authenticate 属性を割り当てます。
[Authenticate]
public HelloResponse Post(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name + " with POST & Auth"};
}
認証を行う BasicAuthProvider クラスから継承します。
public class CustomCredentialsAuthProvider : BasicAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
return userName == "dylan" && password == "abc123";
}
public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
{
session.IsAuthenticated = true;
//Important: You need to save the session!
authService.SaveSession(session, new TimeSpan(0,0,10));
}
}
ご覧のとおり、セッションを保存しますが、10 秒後にタイムアウトします。これは、改善できる可能性があると確信している部分です。でもうまく機能しているようです。
私が達成しようとしていることを行うためのより良い方法はありますか? これらのサービスはセッションレスであるため、Auth、AssignRoles、および UnassignRoles メソッドを削除する方法はありますか?