我的Web应用程序中有两个主要项目:
Web项目按预期工作(身份验证和授权正在运行)
方法:将令牌存储到localstorage,并使用拦截器发送每个请求.
现在我想为WebApi项目添加身份验证和授权,该项目将为Hangfire,Elmah和Help页面等其他模块提供服务.我添加了相同的登录逻辑,它工作(授权),然后重定向到Dashboard页面(使用Angularjs),它工作.
但是去任何其他页面(其中一个提到的模块)都不起作用.不工作:来自Owin上下文的用户总是为空/空.(参见代码)
根据我的理解,我需要以某种方式发送令牌,其中每个请求都不会发生.
问题:
我怎样才能实现这一点(发送/获取令牌)?
如果cookie是唯一/更好的方法↴
如何为项目1和项目2的令牌集成?(尝试使用cookie,但似乎我做错了,或者它与承载令牌同时工作?)
码:
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = new SimpleAuthorizationServerProvider(),
RefreshTokenProvider = new SimpleRefreshTokenProvider()
};
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
GlobalConfiguration.Configure(WebApiConfig.Register);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
AreaRegistration.RegisterAllAreas();
app.UseHangfire(hangfireConfig =>
{
config.UseAuthorizationFilters(
new AuthorizationFilter { Users = "admin, superuser", Roles = "advanced" },
new ClaimsBasedAuthorizationFilter("name", "value")
);
hangfireConfig.UseSqlServerStorage("Context");
hangfireConfig.UseServer();
});
}
Run Code Online (Sandbox Code Playgroud)
我试过测试目的: …
我的Web Api项目有问题.我有文件存储在我的数据库中,并希望直接在新窗口中调用它们来查看/保存(URL如:/ api/Files/5 - 5 beeing FileId)
我正在使用Bearer Token处理我的一般AJAX请求,使用AngularJS获取普通数据,它就像一个魅力.对于该文件,我创建了一个Controller,它在浏览器中显示具有相应MIME类型的文件.但现在我改变了行动[授权]我得到拒绝访问这是正确的,因为我没有通过的HTTP标头的的access_token.
如果可以通过查询字符串传递令牌但是没有找到任何有用的东西,我做了一些研究.
现在我的计划是从我的Controller中删除[Authorize]属性并尝试自己验证令牌,但我不知道如何.
有谁知道我怎么能让它工作?
我有一个 HTML 5 视频标签指向我的 ASP.NET WebAPI,它需要承载身份验证,我对 API 的大多数请求如下所示:
GET http://localhost:29080/api/v1/users/me HTTP/1.1
Host: localhost:29080
Connection: keep-alive
Accept: application/json, text/plain, */*
Origin: http://localhost:4200
Authorization: Bearer c66b36fe-fcc1-49da-9b42-dac783768a06
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
Referer: http://localhost:4200/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,fr;q=0.8
Run Code Online (Sandbox Code Playgroud)
由于应用程序托管在不同的端口(最终是不同的地址)上,因此它受到 CORS 的约束。我已经将我的 WebAPI 设置为兼容的:
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
Run Code Online (Sandbox Code Playgroud)
遗憾的是,我的 HTML 5 视频标签似乎无法使用该设置。
<video
crossorigin="use-credentials"
src="http://localhost:29080/api/v1/entities/470/presentation-video">
Run Code Online (Sandbox Code Playgroud)
我最终得到:
Failed to load http://localhost:29080/api/v1/entities/470/presentation-video:
The value of the 'Access-Control-Allow-Origin' header in the response must
not be the wildcard …
Run Code Online (Sandbox Code Playgroud) angular ×1
angularjs ×1
bearer-token ×1
cookies ×1
cors ×1
javascript ×1
owin ×1
validation ×1