2012-10-20 48 views
5

(我說的是純SS項目,請不要與MVC剃刀混淆)ServiceStack把身份驗證Razor視圖

我們如何限制爲SS剃刀訪問查看與認證?

也就是說,我們如何從SS Razor調用用戶會話和授權碼?

我願做這樣的事情:

@inherits ViewPage 
@Authenticate(RedirectUrl = "/Login") 

<div>Hello @UserSession.UserName</div> 
<div>You are in the secured area now</div> 

回答

3

我不知道任何方式直接從SS剃刀頁面做到這一點的。但是,當我遇到同樣的困境時,我通過創建一個提供頁面的服務來解決它。這樣,您可以使用Authorize屬性來裝飾頁面的服務,如果用戶未通過身份驗證,它將被重定向到登錄頁面。

[Authorize] 
public class MyPageService : IService<MyRequestDTO> 
{ 
    public object Execute(MyRequestDTO request) 
    { 
     ... 
     return new MyPageViewModel(); 
    } 
} 

要檢索剃刀頁當前會話,你可以使用GetSession<T>

@{ 
    var currentSession = GetSession<CustomUserSession>(); 
} 

<div>Hello @currentSession.UserName</div> 

的這另一個好處是,你可以得到一個強類型的視圖,並從服務視圖提供數據。

@inherits ViewPage<MyPageViewModel> 
+0

首先,非常感謝您的回覆。當你說「使用服務接口」時,你的意思是,創建一個基類,如「AuthenticateService」,然後「MyService」繼承該基類。 MyService:AuthenticateService,也有AuthenticateResponse:IhasResponseStatus然後MyResponse:AuthenticateResponse ...就像那樣? – Tom

+0

@Tom,那裏使用了不好的措辭。我的意思是創建一項服務。我編輯了答案。 – Ostemar

+0

謝謝。我現在明白了。 – Tom