【发布时间】:2014-02-01 17:30:44
【问题描述】:
我正在尝试创建一个使用AuthenticateService 自动将用户登录到系统的服务。 (ServiceStack v4.0.8.0)
AppHost配置:
//Plugins
Plugins.Add(new RazorFormat());
Plugins.Add(new SessionFeature());
Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[] { new CustomCredentialsAuthProvider() }));
container.Register<ICacheClient>(new MemoryCacheClient());
我的CustomCredentialsAuthProvider:
public class CustomCredentialsAuthProvider : CredentialsAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
// Custom Auth Logic
// Return true if credentials are valid, otherwise false
// bool isValid = Membership.ValidateUser(userName, password);
return true;
}
public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary<string, string> authInfo)
{
base.OnAuthenticated(authService, session, tokens, authInfo);
var loginManager = authService.TryResolve<LoginManager>();
var loginInfo = loginManager.GetLoginInfo(session.UserAuthName);
authService.SaveSession(loginInfo.CustomUserSession, SessionExpiry);
}
}
我的TestService:
public class TestService : Service
{
public object Any(Test request)
{
var response = new TestResponse();
var authService = base.ResolveService<AuthenticateService>();
var authResponse = authService.Authenticate(new Authenticate
{
UserName = "user",
Password = "password",
RememberMe = false
});
return response;
}
}
当我运行它时,authService 会解析并且TryAuthenticate() 和OnAuthenticated() 方法中的代码执行时不会出错。
最后,当它返回响应并在浏览器中呈现页面时,我看到了这个错误:
“/”应用程序中的服务器错误。
你不需要使用 IHttpRequest.TryResolve 来解决 自己
描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。
异常详细信息:System.Exception:您不需要使用 IHttpRequest.TryResolve 自行解决
来源错误:
在执行过程中产生了一个未处理的异常 当前的网络请求。有关原产地和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。
堆栈跟踪:
[Exception: You don't need to use IHttpRequest.TryResolve<IHttpRequest> to resolve itself]
ServiceStack.Host.AspNet.AspNetRequest.TryResolve() +194
ServiceStack.Formats.MarkdownFormat.GetPageName(Object dto, IRequestrequestContext) +94
ServiceStack.Formats.MarkdownFormat.SerializeToStream(IRequest request, Object response, Stream stream) +284
ServiceStack.Host.<>c__DisplayClass2.<GetResponseSerializer>b__1(IRequest httpReq, Object dto, IResponse httpRes) +92
ServiceStack.HttpResponseExtensionsInternal.WriteToResponse(IResponse response, Object result, ResponseSerializerDelegate defaultAction,
IRequest request, Byte[] bodyPrefix, Byte[] bodySuffix) +2477
[AggregateException: One or more errors occurred.]
System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) +3650617
System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken) +10816173
System.Threading.Tasks.Task.Wait() +10
ServiceStack.Host.Handlers.HttpAsyncTaskHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +83
System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +129
版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.18408
base.ResolveService<T> 应该已经在AuthenticateService 中设置了Request 上下文。但以防万一我也尝试过。
var authService = base.ResolveService<AuthenticateService>();
authService.Request = this.Request;
结果:同样的错误信息。
我也试过这个:
var authService = base.ResolveService<AuthenticateService>();
authService.Request = System.Web.HttpContext.Current.ToRequest();
结果:我没有收到任何错误,而且我看到了 TestResponse!另外,我取回了“ss-id”和“ss-pid”cookie。
我会在这里停下来,但是如果我向具有[Authenticate] 属性的服务发出请求,即使我有 cookie。我明白了:
未找到请求的处理程序:
Request.HttpMethod: 获取
Request.PathInfo: /login
Request.QueryString:ServiceStack.NameValueCollectionWrapper
Request.RawUrl:/login?redirect=http%3a%2f%2flocalhost%3a50063%2fBOP%2fbasic-info-2
所以它没有正确验证。
【问题讨论】:
标签: authentication servicestack