https://github.com/ServiceStack/ServiceStack.UseCases/tree/master/CustomAuthenticationMvcの例に従って、MVC と ServiceStack に対して認証を試みています。私の問題は、アカウント/ログオンへの最初の要求で ServiceStack に対して正常に認証できないことです。
AccountController の LogOn メソッド内の ServiceStack 関連のコード:
var apiAuthService = AppHostBase.Resolve<AuthService>();
apiAuthService.RequestContext = System.Web.HttpContext.Current.ToRequestContext();
var apiResponse = apiAuthService.Authenticate(new Auth
{
UserName = model.UserName,
Password = model.Password,
RememberMe = false
});
CredentialsAuthProvider をサブクラス化するカスタム認証プロバイダーがあります。AppHost クラスで次のように構成します。
var appSettings = new AppSettings();
Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[] {
new ActiveDirectoryAuthProvider(),
}));
public override bool TryAuthenticate(ServiceStack.ServiceInterface.IServiceBase authService, string userName, string password)
{
//class to authenticate against ActiveDirectory
var adAuthentication = new ActiveDirectoryAuthenticationService();
if (!adAuthentication.Authenticate(userName, password))
return false;
var session = (CustomUserSession)authService.GetSession(false);
session.IsAuthenticated = true;
session.UserAuthId = session.UserAuthName;
authService.SaveSession(session, SessionExpiry);
return true;
}
私の問題は、この時点でsession.Idが null であり、セッションを保存すると「urn:iauthsession:」が「SessionCache」に保持されることだと思います。ただし、 session.Idを正しく入力する方法がわかりません。また、これは問題になる場合とそうでない場合がありますが、最初のログオン要求は、MVC によって処理されるアカウント/ログオンに対するものです。そのため、AccountController で AuthService.Authenticate() を呼び出す前に、ServiceStack へのリクエストはありません。
私が思いついた解決策は、CredentialsAuthProvider のサブクラスの下に追加されています。
public override bool TryAuthenticate(ServiceStack.ServiceInterface.IServiceBase authService, string userName, string password)
{
//class to authenticate against ActiveDirectory
var adAuthentication = new ActiveDirectoryAuthenticationService();
if (!adAuthentication.Authenticate(userName, password))
return false;
var session = (CustomUserSession)authService.GetSession(false);
//A possible solution???
if(session.Id == null)
{
var req = authService.RequestContext.Get<IHttpRequest>();
var sessId = HttpContext.Current.Response.ToResponse().CreateSessionIds(req);
session.Id = sessId;
req.SetItem(SessionFeature.SessionId, sessId);
}
//end possible solution
session.IsAuthenticated = true;
session.UserAuthId = session.UserAuthName;
authService.SaveSession(session, SessionExpiry);
return true;
}
MVC 内で ServiceStack 認証を「接続」するために欠落している構成または呼び出しはありますか?
ありがとう。