2013-07-18 3 views
2

사용자 정의 인증 공급자를 사용하여 ServiceStack API를 설정했습니다. 이 모든 것은 정상적으로 작동하며 내가 원하는대로 API를 인증하고 사용할 수 있습니다.MVC 4의 영구 ServiceStack 인증 양식 인증

FormsAuthentication과 함께 별도의 MVC4 응용 프로그램이 있는데이 API를 사용하여 사용자의 유효성을 검사합니다. Api에서 사용자 이름/비밀번호를 확인한 후 클라이언트의 모든 후속 요청을 유효하게 유지하려고합니다. 클라이언트가 쿠키를 CookieContainer에 설정하고 동일한 클라이언트를 유지하면 모든 요청이 유효하게 유지됩니다.

var client = new JsonServiceClient(AppConfig.Settings.ApiEndpoint); 
client.Headers.Add("X-ApiKey", AppConfig.Settings.ApiKey); 
auth.RememberMe = true; 
AuthResponse authResponse = client.Post(auth); 

그러나 요청할 때마다 API에 인증을 게시하고 싶지 않습니다 (옵션 임에도 불구하고). 나는이 요점이 어디서 발생하는지 https://gist.github.com/danmiser/1701803 (내가 생각하는 ServiceStack의 구버전)을 발견했다.

또한 MVC4 앱의 클라이언트 측에서 반환되는 쿠키를 설정하고 인증 후 후속 요청을 위해 클라이언트의 CookieContainer에 추가하는 것에 대해 생각해 보았습니다.

더 나은/표준 방법이 있습니까? 나는 명백한 것을 놓치고 있는가?

답변

3

세션 쿠키로 인증 된 JsonServiceClient를 반환하기 위해 '도우미 메서드'를 사용했습니다. 인증 된 쿠키 값 **을 저장하고 제공하는 방법을 찾아야합니다. 가장 쉬운 방법은 MVC 세션에 저장하는 것입니다.

public JsonServiceClient GetAuthenticatedClient() 
    { 
     //Need to get an authenticated session key/token from somewhere 
     //this can be used when MVC and ServiceStack are running together-> var sessionKey = SessionFeature.GetSessionKey().Replace("urn:iauthsession:", ""); 
     //maybe add the key/token to a Session variable after your first successful authentication??? 
     var sessionKey = Session["someKey"] 

     var client = new JsonServiceClient("http://" + HttpContext.Request.Url.Authority + "/api") 
     { 
      LocalHttpWebRequestFilter = (r) => 
      { 
       var c = new CookieContainer(); 
       c.Add(new Uri("http://" + HttpContext.Request.Url.Authority + "/api"), new Cookie() { Name = "ss-id", Value = sessionKey }); 
       r.CookieContainer = c; 
      } 
     }; 

     return client; 
    } 

** 당신이 ServiceStack의 API에 대한 인증에 사용하는 client의 값 ... 내가 현재의 cookiecontainer를 저장, 그 일을 결국 어떻게 정확히 Session["someKey"] = client.CookieContainer.GetCookies(uri)["ss-id"].Value

+0

뭔가를 얻을 수 있습니다 로그인 한 사용자. 내 답변을 추가하는 것을 잊어 버렸지 만, 귀하의 답변이 정확히 같기 때문에이 답변을 수락합니다. – Taesti