1 Star 1 Fork 0

李波 / lan.hanshu.webapi

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

lan.hanshu.webapi

介绍

WebApi 缓存 授权 验证

软件架构

静态函数

安装教程

  1. nuget lan.hanshu.webapi

  2. web.config

<system.web>
    <compilation debug="true" targetFramework="4.5.2" >
      <assemblies>
        <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5.2" />
  </system.web>

使用说明

  1. 注册配置
protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //使用Redis缓存器
            lan.hanshu.Registration.RegisterAllConfigWithRedis("localhost", 7);

            //MyApiIdentityCache 实现接口 IApiIdentityCache
            //lan.hanshu.Registration.RegisterAllConfig(new MyApiIdentityCache()); 
        }

2.控制器

    [RoutePrefix("api/values")]
    public class ValuesController : ApiController
    {
        [HttpPost]
        [HttpOptions]//跨域需要加这项
        [Route("login")]
        public ApiResult<string> Login()
        {
            using (var builder = new ApiResultBuilder<string>(string.Empty))
            {
                builder.data = User.NameToUserToken("userid", "authcode");
                return builder.Successed();
            }
        }
        [HttpGet]
        [HttpOptions]
        [Route("mybooks")]
        [ApiIdentityFilter] //身份认证
        //[ApiIdentityFilter("authcode")] 需要权限验证
        public ApiResult<PageModel<string>> MyBooks()
        {

            using (var builder = new ApiResultWithPageBuilder<string>(0, 0))//limit,offset
            {
                var userid = User.Identity.Name;

                builder.data.total = 0;
                builder.data.data = new string[0];
                return builder.Successed();
            }

            //using (var builder = new ApiResultWithPageBuilder<string,ent>(0, 0))//limit,offset
            //{
            //    var userid = User.Identity.Name;
            //    var books =  builder.Database.BookInfos.Where(q=>q.UserId == userid).Select(q=>q.Name).ToList();
            //    builder.data.total = 0;
            //    builder.data.data = new string[0];
            //    return builder.Successed();
            //}
        }
    }

3.返回模型

//请求结果状态(code) 
//1200表示成功, 
//1400表示已知错误(请直接向用户展示错误描述信息), 
//1401表示用户身份验证失败(例如:未登录), 
//1403表示权限验证失败(例如:非会员), 
//1406表示请求数据验证失败(例如:必填项为空), 
//1500表示未知错误(BUG)

{
  "code": 1,
  "msg": "sample string 2",
  "data": {
    "total": 1,
    "data": [
      "sample string 1",
      "sample string 2"
    ]
  }
}

4.前端身份认证

Headers X-User-Token 

5.HelpPage

delete file XmlDocumentationProvider.cs

delete file IModelDocumentationProvider.cs

public static void Register(HttpConfiguration config)
        {
            //// Uncomment the following to use the documentation from XML documentation file.
            config.SetDocumentationProvider(new lan.hanshu.MultiXmlDocumentationProvider(
                HttpContext.Current.Server.MapPath("~/bin/lan.hanshu.webapi.xml"),//注册XML
                HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
                ...
        }
...
using lan.hanshu;//引用 IModelDocumentationProvider

namespace WebApplication.Areas.HelpPage.ModelDescriptions
{
        public class ModelDescriptionGenerator
        {
            ...
        }
}
 public class ModelDescriptionGenerator
    {
        // Modify this to support more data annotation attributes.
        private readonly IDictionary<Type, Func<object, string>> AnnotationTextGenerator =
lan.hanshu.ApiExplorer.AnnotationTextGenerator;
        ...
    }
public class HelpController : Controller
    {
        ...
        public ActionResult Index()
        {
            ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
            var apiDescriptions =
lan.hanshu.ApiExplorer.GetApiDescriptionsExcludeOptions(Configuration.Services.GetApiExplorer());
            return View(apiDescriptions);
        }
        ...
    }

6.日志

nuget nlog.config

参与贡献

  1. inmacro

Comments ( 0 )

Sign in for post a comment

About

WebApi 缓存 授权 验证 spread retract
C#
MIT
Cancel

Releases

No release

Contributors

All

Activities

load more
can not load any more
C#
1
https://gitee.com/inmacro/lan.hanshu.webapi.git
git@gitee.com:inmacro/lan.hanshu.webapi.git
inmacro
lan.hanshu.webapi
lan.hanshu.webapi
master

Search