我正在尝试使用OWIN中间件为我的ASP.NET WebAP实现JWT身份验证
Startup.cs类
public void Configuration(IAppBuilder app)
{
// // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
// ConfigureAuth(app);
// app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
ConfigureAuth(app);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
var config = new HttpConfiguration();
WebApiConfig.Register(config);
// config.Filters.Add(new ClientPortalAuthorizeAttribute());
app.UseWebApi(config);
StartUp.cs类
public partial class Startup
{
public static string externalAadInstance = ConfigurationManager.AppSettings["external:AadInstance"];
public static string externalTenant = ConfigurationManager.AppSettings["ida:Tenant"];
public static string internalMetadataEndpoint = ConfigurationManager.AppSettings["internal:metadataEndPoint"];
public void ConfigureAuth(IAppBuilder app)
{
var metadataEndpoint = string.Format(externalAadInstance, externalTenant, "B2C_1_SignIn-SignUp");
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
{
AccessTokenFormat = new JwtFormat(new TokenValidationParameters
{
// This is where you specify that your API only accepts tokens from its own clients
ValidAudience = ConfigurationManager.AppSettings["ida:Audience"],
AuthenticationType = "B2C_1_SignIn-SignUp",
NameClaimType = "http://schemas.microsoft.com/identity/claims/objectidentifier"
}, new OpenIdConnectCachingSecurityTokenProvider(metadataEndpoint))
});
}
}
UI代码::
function getTodoList(accessToken, dataContainer, loading) {
// Get TodoList Data
$.ajax({
type: "GET",
url: "https://localhost:44327/api/TodoList",
headers: {
'Authorization': 'Bearer ' + accessToken,
// 'Access-Control-Allow-Origin': '*',
//'Access-Control-Allow-Methods': 'GET,POST',
//'Access-Control-Allow-Headers': 'Authorization, Content-Type',
},
}).done(function (data) {
当我们尝试从Ui点击web api然后收到消息(“消息”:“此请求已拒绝授权。”
请帮助我,它的决心。