c# 如何写mvc接口
public class ApiAuthorizeAttribute : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext
)
{
var authHeader
= from t
in actionContext
.Request.Headers where t
.Key
== "auth" select t
.Value
.FirstOrDefault();
if (authHeader
!= null)
{
string token
= authHeader
.FirstOrDefault();
if (!string.IsNullOrEmpty(token
))
{
try
{
string secret
= ConfigurationManager
.AppSettings
["AppScret"].ToString();
IJsonSerializer serializer
= new JsonNetSerializer();
IDateTimeProvider provider
= new UtcDateTimeProvider();
IJwtValidator validator
= new JwtValidator(serializer
, provider
);
IBase64UrlEncoder urlEncoder
= new JwtBase64UrlEncoder();
IJwtDecoder decoder
= new JwtDecoder(serializer
, validator
, urlEncoder
);
var json
= decoder
.DecodeToObject<AuthInfo>(token
, secret
, verify
: true);
if (json
!= null)
{
actionContext
.RequestContext
.RouteData
.Values
.Add("auth", json
);
return true;
}
return false;
}
catch (Exception ex
)
{
return false;
}
}
}
return false;
}
}
[ApiAuthorize]
[AcceptVerbs("POST")]
public ResponseData GetEapushHistoryDetailedInformationData([FromBody]RecordRequest request
)
[ApiAuthorize]
[AcceptVerbs("GET")]
public ResponseData GetEapushHistoryDetailedInformationData(string recordid
)
[ApiAuthorize]
[AcceptVerbs("GET", "POST")]
public ResponseData GetEapushHistoryDataTable([FromBody]TableRequest request
)
public class ResponseData
{
public bool Success
{ get; set; }
public string Message
{ get; set; }
public string Opcode
{ get; set; }
public string Data
{ get; set; }
public int Count
{ get; set; }
}
[EnableCors(origins
: "http://47.93.xxxx.xxx:xxxx", headers
: "*", methods
: "GET,POST,PUT,DELETE")]
转载请注明原文地址:https://tech.qufami.com/read-5979.html