• owinAuthorize


    Nuget包获取

    Install-Package Microsoft.AspNet.WebApi.Owin -Version 5.1.2
    Install-Package Microsoft.Owin.Host.SystemWeb -Version 2.1.0
    Install-Package Microsoft.AspNet.Identity.Owin -Version 2.0.1
    Install-Package Microsoft.Owin.Cors -Version 2.1.0
    Install-Package EntityFramework -Version 6.0.0 
    

      添加OwinStartUp类

    using System;
    using System.Web.Http;
    
    using Owin;
    using Microsoft.Owin;
    using Microsoft.Owin.Security.OAuth;
    using SqlSugar.WebApi;
    
    [assembly: OwinStartup(typeof(WebApi.Startup))]
    namespace WebApi
    {
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                HttpConfiguration config = new HttpConfiguration();
                ConfigureOAuth(app);
    
                WebApiConfig.Register(config);
                app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
                app.UseWebApi(config);
            }
    
            public void ConfigureOAuth(IAppBuilder app)
            {
                OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
                {
                    AllowInsecureHttp = true,
                    TokenEndpointPath = new PathString("/token"),
                    AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
                    Provider = new SimpleAuthorizationServerProvider()
                };
                app.UseOAuthAuthorizationServer(OAuthServerOptions);
                app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
            }
        }
    }

    添加验证类SimpleAuthorizationServerProvider

    using System.Threading.Tasks;
    using System.Security.Claims;
    using Microsoft.Owin.Security.OAuth;
    
    namespace WebApi
    {
        /// <summary>
        /// Token验证
        /// </summary>
        public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
        {
            public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
            {
                await Task.Factory.StartNew(() => context.Validated());
            }
    
            public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
            {
                await Task.Factory.StartNew(() => context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }));
                /*
                 * 对用户名、密码进行数据校验
                using (AuthRepository _repo = new AuthRepository())
                {
                    IdentityUser user = await _repo.FindUser(context.UserName, context.Password);
    
                    if (user == null)
                    {
                        context.SetError("invalid_grant", "The user name or password is incorrect.");
                        return;
                    }
                }*/
    
                var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                identity.AddClaim(new Claim("sub", context.UserName));
                identity.AddClaim(new Claim("role", "user"));
    
                context.Validated(identity);
    
            }
        }
    }

    添加验证

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    
    namespace WebApplication1.Controllers
    {
        [Authorize]
        public class ValuesController : ApiController
        {
            // GET api/values
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }
    
            // GET api/values/5
            public string Get(int id)
            {
                return "value";
            }
    
            // POST api/values
            public void Post([FromBody]string value)
            {
            }
    
            // PUT api/values/5
            public void Put(int id, [FromBody]string value)
            {
            }
    
            // DELETE api/values/5
            public void Delete(int id)
            {
            }
        }
    }

    获取token

    调接口bearer Token

     https://www.cnblogs.com/Hai--D/p/6187051.html

  • 相关阅读:
    easyui combobox 获取焦点
    easyui combobox keyhandler使用
    easyui combobox 取值
    Tomcat启动超时问题Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
    spring定时器中如何获取servletcontext
    不用asp.net MVC,用WebForm照样能够实现MVC
    打印从1到最大的n位数
    [NOIP复习]第三章:动态规划
    Java并发与同步
    Bootstrap的js插件之按钮(button)
  • 原文地址:https://www.cnblogs.com/chenyishi/p/8471861.html
Copyright © 2020-2023  润新知