我想讓服務堆服務只有授權用戶纔可訪問。這是我的註冊方法var authResponse = client.Post(new Authenticate { provider = CredentialsAuthProvider.Name, //= credentials UserName = model.Username, Password = model.Password, RememberMe = true, }); client.UserName = model.Username; client.Password = model.Password;
未經授權的例外,ServiceStack
這是登錄方法
[HttpPost]
[ActionName("LogIn")]
public ActionResult Login(LogInModel model)
{
var authResponse = client.Post(new Authenticate
{
provider = CredentialsAuthProvider.Name, //= credentials
UserName = model.Username,
Password = model.Password,
RememberMe = true,
});
client.UserName = model.Username;
client.Password = model.Password;
return RedirectToAction("RefundPayment");
}
APPHOST:
public override void Configure(Container container)
{
SetConfig(new HostConfig
{
HandlerFactoryPath = "api",
});
//Config examples
//this.Plugins.Add(new PostmanFeature());
//this.Plugins.Add(new CorsFeature());
//Set MVC to use the same Funq IOC as ServiceStack
ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[] {
new BasicAuthProvider(), //Sign-in with HTTP Basic Auth
new CredentialsAuthProvider(), //HTML Form post of UserName/Password credentials
}));
Plugins.Add(new RegistrationFeature());
container.Register<ICacheClient>(new MemoryCacheClient());
var userRep = new InMemoryAuthRepository() ;
container.Register<IUserAuthRepository>(userRep);
}
爲什麼會出現這種異常,儘管我在登錄驗證用戶在方法?我必須在AppHost中更改一些代碼嗎?謝謝。
感謝您的幫助。當我重定向到另一個動作時,它仍會引發未經授權的異常,我是否必須在每個控制器中進行身份驗證? –
@MartynasBrazionis你只需要驗證一次,然後你就可以調用受ServiceStack的'[Authenticate]'屬性保護的控制器,如http://mvc.servicestack.net演示中所示。注意你的控制器應該繼承'ServiceStackController'。 – mythz