c# 如何写mvc接口

tech2022-10-02  120

c# 如何写mvc接口

//.net身份认证 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(); //secret需要加密 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")]
最新回复(0)