我在請求主體中傳遞了祕密API密鑰以及用戶名和密碼。一旦授權,就會生成一個令牌,並且客戶端必須將其傳遞給授權標頭。這將在每個請求的基礎控制器中進行檢查。
- 客戶端調用myapp.com/authorize返回auth令牌。
- 客戶端本地存儲auth令牌。
- 客戶端調用myapp.com/anycontroller,並在授權標頭中使用authtoken。
AuthorizeController來自控制器繼承。 Anycontroller繼承自執行授權代碼的自定義基礎控制器。
我的示例需要以下的路徑,其引導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的背景),所以我就如何授權屬性工作模糊。謝謝! – 2011-05-01 22:55:38
謝謝傑森!你是如何「傳之祕API密鑰與請求主體的用戶名和密碼一起」?你只是添加自定義的HTTP頭到請求或者你使用Http-Authorize? – 2011-05-01 23:20:09
非常好,謝謝你的詳細回覆! – 2011-05-02 04:17:16