/// /// /// public class CustomOAuthProvider : OAuthAuthorizationServerProvider {}
其次,重写虚方法,实现验证逻辑。
/// /// /// /// /// public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var username = context.UserName; var password = context.Password; string userid; if (!CheckCredential(username, password, out userid)) { context.SetError("invalid_grant", "The user name or password is incorrect"); context.Rejected(); return Task.FromResult
自定义一个AccessTokenFormat:
/// /// 自定义 jwt token 的格式 /// public class CustomJwtFormat : ISecureDataFormat { private readonly byte[] _secret; private readonly string _issuer; public CustomJwtFormat(string issuer, byte[] secret) { _issuer = issuer; _secret = secret; } /// /// /// /// /// public string Protect(AuthenticationTicket data) { var secret = TextEncodings.Base64Url.Decode("IxrAjDoa2FqElO7IhrSrUJELhUckePEPVpaePlS_Xaw");//秘钥 SecurityKey securityKey = new InMemorySymmetricSecurityKey(secret); var signingKey = new InMemorySymmetricSecurityKey(_secret); var issued = data.Properties.IssuedUtc; var expires = data.Properties.ExpiresUtc; return new JwtSecurityTokenHandler().WriteToken(new JwtSecurityToken(_issuer, "Any", data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, new SigningCredentials(securityKey, "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", "http://www.w3.org/2001/04/xmlenc#sha256"))); } /// /// /// /// /// public AuthenticationTicket Unprotect(string protectedText) { var jst = new JwtSecurityTokenHandler(); var token = (JwtSecurityToken)jst.ReadToken(protectedText); var identity = new ClaimsIdentity(token.Claims); var ticket = new AuthenticationTicket(identity, new AuthenticationProperties() { }); return ticket; } }
最后,创建一个启动类,配置中间件。
/// /// /// /// public void Configuration(IAppBuilder app) { HttpConfiguration config = new HttpConfiguration(); var issuer = "http://localhost:2817/"; var audience = "fNm0EDIXbfuuDowUpAoq5GTEiywV8eg0TpiIVnV8";//观众 var secret = TextEncodings.Base64Url.Decode("IxrAjDoa2FqElO7IhrSrUJELhUckePEPVpaePlS_Xaw");//秘钥 var signingKey = new System.IdentityModel.Tokens.InMemorySymmetricSecurityKey(secret); //配置JwtBearer授权中间件 app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions() { AuthenticationMode = AuthenticationMode.Active, AuthenticationType = "password", AllowedAudiences = new[] { audience }, Provider = new OAuthBearerAuthenticationProvider() { OnValidateIdentity = context => { context.Ticket.Identity.AddClaim(new System.Security.Claims.Claim(ClaimTypes.NameIdentifier, context.Ticket.Identity.Name)); return Task.FromResult(context); }, OnRequestToken = (context) => { if (context.Token != null) { context.Response.Headers["WWW-Authorization"] = "Bearer"; var jst = new JwtSecurityTokenHandler(); var token = (JwtSecurityToken)jst.ReadToken(context.Token); //创建身份 var identity = new ClaimsIdentity(token.Claims, "JWT"); context.Request.User = new ClaimsPrincipal(identity); } return Task.FromResult(context); } }, IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[] { new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret) } }); //配置OAuth中间件 app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions { //生产环境设为false AllowInsecureHttp = true, TokenEndpointPath = new PathString("/oauth2/token"),//请求token的路径 AccessTokenExpireTimeSpan = TimeSpan.FromDays(30),//令牌的过期时间 //请求获取token时,验证username, password Provider = new CustomOAuthProvider(), //定义token信息格式 --默认TicketDataFormat AccessTokenFormat = new CustomJwtFormat(issuer, secret), }); app.UseWebApi(config);// }
[Authorize] public class ValuesController : ApiController { // GET api/values public IEnumerable Get() { var owin = HttpContext.Current.GetOwinContext(); var identity = owin.Request.User; return new string[] { "value1", "value2" }; }}