0
我只能在客戶端進行身份驗證時才能訪問服務,我只能訪問Authenticate屬性,但因爲無法驗證而無法訪問服務,所以無法正常工作。我將Request Authenticate屬性放在請求DTO,服務的頂部和Action之前。下面是我想保護ServiceStack驗證屬性不檢查用戶是否通過驗證
[Authenticate]
public class HelloService : Service
{
public const string HelloServiceCounterKey = "HelloServiceCounter";
public object Any(HelloRequest request)
{
var userSession = SessionAs<AppHost.CustomUserSession>();
Session.Set(HelloServiceCounterKey, Session.Get<int>(HelloServiceCounterKey) + 1);
var roles = string.Join(", ", userSession.Roles.ToArray());
return new HelloResponse { Result = "Hello, " + request.Name + ", your role(s): " + roles };
}
}
我有這樣的服務的一些代碼我APPHOST配置(Funq.Container容器)
Plugins.Add(new AuthFeature(
() => new CustomUserSession(),
new[] { new CustomCredentialsAuthProvider() }
));
public class CustomUserSession : AuthUserSession
{
public string CompanyName { get; set; }
}
public class CustomCredentialsAuthProvider : CredentialsAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
if (!Membership.ValidateUser(userName, password)) return false;
var session = (CustomUserSession)authService.GetSession(false);
session.CompanyName = "Company from DB";
session.UserAuthId = userName;
session.IsAuthenticated = true;
// add roles
session.Roles = new List<string>();
if (session.UserAuthId == "admin") session.Roles.Add(RoleNames.Admin);
session.Roles.Add("User");
return true;
}
}
當訪問在
var roles = string.Join(", ", userSession.Roles.ToArray());
行服務
obviosly返回NULL,因爲尚未進行身份驗證。
應該怎麼做這種情況下的驗證屬性?
我已經配置我的身份驗證提供者CustomCredentialsAuthProvider和我添加的代碼到原來的問題@pedro希望你能幫助我 –
仍然不工作,我仍然可以不認證@Pedro –
訪問該服務去看一下我的更新 –