使用web中未经授权的调用的例子,它将根据此提供响应。
发布于 2017-02-27 00:43:20
是的,只要使用,如果您想要更改从服务器返回的Josn响应的结构,您可以使用asp.net mvc应用程序中的折叠代码创建新的响应。
// here you can use your own properties which then can be send to client .
return Json(new { Status= false ,Description = response.Message });
如果您有控制器方法,那么您应该返回JsonResult
如果您正在寻找一个通用的解决方案,那么请看一看本文,它可能会对您有所帮助。
发布于 2017-02-27 00:45:15
这可以通过定制的AuthorizeAttribute来完成。
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public CustomAuthorizeAttribute ()
{
}
public override void OnAuthorization(HttpActionContext actionContext)
{
try
{
if (Authorize(actionContext))
{
return;
}
HandleUnauthorizedRequest(actionContext);
}
catch (Exception)
{
//create custom response
actionContext.Response = actionContext.Request.CreateResponse(
HttpStatusCode.OK,
customresponse
);
return;
}
}
protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
{
//create custom unauthorized response
actionContext.Response = actionContext.Request.CreateResponse(
HttpStatusCode.OK,
customunauthorizedresponse
);
return;
}
private bool Authorize(HttpActionContext actionContext)
{
//authorization logics
}
}
在api控制器方法中,可以使用[CustomAuthorizeAttribute]
insted of [Authorize]
。
https://stackoverflow.com/questions/42480673
复制