요청 본문에 사용자 이름과 비밀번호와 함께 비밀 API 키를 전달합니다. 권한이 부여되면 토큰이 생성되고 클라이언트는 Authorization 헤더에 토큰을 전달해야합니다. 이것은 각 요청에 대해 기본 컨트롤러에서 확인됩니다.
- 클라이언트는 인증 토큰을 반환하는 myapp.com/authorize를 호출합니다.
- 클라이언트가 인증 토큰을 로컬에 저장합니다.
- 클라이언트는 인증 헤더에 authtoken과 함께 myapp.com/anycontroller를 호출합니다.
AuthorizeController는 컨트롤러를 상속합니다. Anycontroller는 인증 코드를 수행하는 사용자 지정 기본 컨트롤러에서 상속합니다.
이 예제에서는 POST 요청을 모든 컨트롤러의 POST라는 ActionResult에 전달하는 다음 경로가 필요합니다. 나는 이것을 당신이 일반적인 생각을 가능한 한 많이 단순화하기 위해 직접 입력하고있다. 잘라 내기 및 붙여 넣기 및 작동 :)
는
routes.MapRoute(
"post-object",
"{controller}",
new { controller = "Home", action = "post" {,
new { httpMethod = new HttpMethodConstraint("POST")}
);
귀하의 인증 컨트롤러는이
public class AuthorizationController : Controller
{
public ActionResult Post()
{
string authBody;
var request = ControllerContext.HttpContext.Request;
var response = ControllerContext.HttpContext.Response;
using(var reader = new StreamReader(request.InputStream))
authBody = reader.ReadToEnd();
// authorize based on credentials passed in request body
var authToken = {result of your auth method}
response.Write(authToken);
}
}
귀하의 다른 컨트롤러가 기본 컨트롤러
public class BaseController : Controller
{
protected override void Execute(RequestContext requestContext)
{
var request = requestContext.HttpContext.Request;
var response = requestContext.HttpContext.Response;
var authToken = Request.Headers["Authorization"];
// use token to authorize in your own method
var authorized = AmIAuthorized();
if(authorized = false) {
response.StatusCode = 401;
response.Write("Invalid token");
return;
}
response.StatusCode = 200; // OK
base.Execute(requestContext); // allow inheriting controller to continue
}
}
샘플에서 상속 사용할 수 있습니다 기대하지 마십시오 API 호출 코드
public static void ExecutePostRequest(string contentType)
{
request = (HttpWebRequest)WebRequest.Create(Uri + Querystring);
request.Method = "POST";
request.ContentType = contentType; // application/json usually
request.Headers["Authorization"] = token;
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
writer.Write(postRequestData);
// GetResponse reaises an exception on http status code 400
// We can pull response out of the exception and continue on our way
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = (HttpWebResponse)ex.Response;
}
finally
{
using (StreamReader reader =
new StreamReader(response.GetResponseStream()))
responseText = reader.ReadToEnd();
httpcontext = HttpContext.Current;
}
}
제이슨, 나는 더 자세한 것을 좋아할거야. 저는 MVC (RoR 배경)에 익숙하지 않아서 속성의 권한 부여 방식이 어색합니다. 감사! –
고맙습니다 Jason! 요청 본문에 사용자 이름과 비밀번호와 함께 비밀 API 키를 전달하는 방법은 무엇입니까? 요청에 사용자 정의 HTTP 헤더를 추가하거나 Http-Authorize를 사용합니까? –
우수한 답변을 주셔서 감사합니다. –