我使用了一个restconsole发布:
http://MyApp/auth/credentials
我的自定义AuthProvider如下:
public class MyAppCredentialsAuthProvider : 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
SetUp();
var userDao = _appHost.Container.Resolve<IUserDao>();
bool isValid = false;
if (string.IsNullOrEmpty(userName))
return false;
if (string.IsNullOrEmpty(password))
return false;
var user = userDao.GetUserByUsername(userName);
if (user == null)
return false;
if (string.IsNullOrEmpty(user.Password))
return false;
if (CheckPassword(password, user.Password))
{
bool isApproved = user.IsApproved;
if (isApproved && user.Status == (short)Status.Active)
{
isValid = true;
user.IsOnLine = true;
user.LastLoginDate = DateTime.Now;
userDao.Update(user);
}
}
return isValid;
}
public override void OnAuthenticated(IServiceBase authService,
IAuthSession session, IOAuthTokens tokens,
Dictionary<string, string> authInfo)
{
try
{
// Authenticate the user.
var userRepo = authService.TryResolve<IUserDao>();
var user = userRepo.GetUserByUsername(session.UserAuthName);
session.LastName = user.Lastname;
session.UserName = user.Username;
session.Email = user.UserEmail;
session.DisplayName = user.Lastname + " " + user.Othernames;
session.IsAuthenticated = true;
//Important: You need to save the session!
SessionExpiry = new TimeSpan(0, 5, 0);
authService.SaveSession(session, SessionExpiry);
}
catch (Exception exc)
{
// log error
}
}
public void SetUp()
{
_appHost = new BasicAppHost().Init();
var container = _appHost.Container;
var dbFactory = new OrmLiteConnectionFactory(ConfigurationManager.ConnectionStrings["MyAppDB"].ConnectionString, SqlServerDialect.Provider);
container.Register<IDbConnectionFactory>(dbFactory);
container.RegisterAutoWiredAs<UserDao, IUserDao>();
}
}
执行后存在公共覆盖void OnAuthenticated(...)
我收到以下错误:
Server Error in '/' Application.
Object reference not set to an instance of an object.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
ServiceStack.WebHost.Endpoints.Support.EndpointHandlerBase.HandleException(IHttpRequest httpReq, IHttpResponse httpRes, String operationName, Exception ex) +206
ServiceStack.WebHost.Endpoints.RestHandler.ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, String operationName) +1058
ServiceStack.WebHost.Endpoints.Support.EndpointHandlerBase.ProcessRequest(HttpContext context) +264
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +341
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
我错过了哪些参数或配置或步骤?
感谢。