, 우리는 응답이 컨트롤러 내에서 처리 할 수 있도록하기 전에 요청을 인증 할 필요가 있고, 그래서 이것은뿐만 아니라 헤더를 읽을 수 할 필요를 생성 요청 헤더가 아닌 양식 본문 내의 요청에 자격 증명이 전달되었는지 확인하기위한 양식 (있는 경우). MVC 컨트롤러에
public class WebServiceAuthenticationAttribute : AuthorizationFilterAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
var authenticationHeaderValue = actionContext.Request.Headers.Authorization;
try
{
if (authenticationHeaderValue != null)
{
var webRequestInfo = new WebRequestInfo(actionContext.Request.Method, actionContext.Request.RequestUri);
this.AuthenticationHeaderService.LogOnUsingAuthenticationHeader(authenticationHeaderValue, webRequestInfo);
}
else if (actionContext.Request.Content.IsFormData())
{
Task<NameValueCollection> formVals = actionContext.Request.Content.ReadAsFormDataAsync();
this.AuthenticationFormService.LogOnUsingFormsAuthentication(formVals.Result);
// reset the underlying stream to the beginning so that others may use it in the future...
using (var s = new System.IO.MemoryStream())
{
var ctx = (HttpContextBase) actionContext.Request.Properties["MS_HttpContext"];
ctx.Request.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
}
}
}
catch (Exception)
{
throw;
}
}
}
초기 데이터 모델이 아니었다 뷰 모델을 양식을 읽고 채울 수있을 것입니다 수 있도록 코드의
몇 라인은 스트림의 시작 부분에 스트림 포인터를 재설정 MVC에 의해 생성되고 널 (null)이 제어기 메소드에 전달되었습니다. 스트림을 재설정 한 후 MVC는 양식을 읽고 데이터 모델을 만들고 채우고 컨트롤러 메서드에 전달할 수있었습니다.
[WebServiceAuthentication]
public HttpResponseMessage Get(DocumentRequestModel requestForm)
{
var response = CreateResponse(HttpStatusCode.OK);
response.Content = new ByteArrayContent(this.documentService.GetDocument(requestForm.DocumentId.ToString()));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;
}
재현을 만드는 데 도움이 되겠습니까? – Aliostad