我的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)
我试过测试目的: …
假设有这个模板(用于父组件)
<button ... (click)="Save()">
...
<ngb-tabset [activeId]="selectedTab" #tabs>
<ngb-tab id="tab1">
<template ngbTabTitle>tab1</template>
<template ngbTabContent>
<child-comp1 #comp1>
</child-comp1>
</template>
</ngb-tab>
<ngb-tab id="tab2">
<template ngbTabTitle>tab2</template>
<template ngbTabContent>
<child-comp2 #comp2>
</child-comp2>
</template>
</ngb-tab>
...
</ngb-tabset>
Run Code Online (Sandbox Code Playgroud)
在每个子组件(child-comp1 ...)中,我有一个表单和带有一些验证的输入。
如何按需从父组件访问子组件的方法,我的意思是这样的:
Save(){
if(Validate()){
//Save an object ...
}
}
Validate(){
if(!this.comp1.Validate()){
// Activate tab1
return false;
}
else if(!this.comp2.Validate()){
// Activate tab2
return false;
}
//...
return true;
}
Run Code Online (Sandbox Code Playgroud)
在父组件中,我有:
// imports ...
@Component({ ... })
export class parent implements OnInit {
@ViewChild(ChildComp) comp1: …
Run Code Online (Sandbox Code Playgroud) 我有一个带有延迟加载模块的应用程序。
在一个特定的延迟加载模块中,我有一个父组件,它具有router-outlet
.
在该父组件中,我有一个按钮,它基本上使用共享服务(在延迟加载模块上提供)发出事件。
每个子组件(在 中加载router-outlet
)都使用相同的共享服务订阅该事件!
我尝试使用英雄演示在以下plunkr 中重新创建我的用例。
打开开发工具以查看console
消息。
Heroes
链接(默认情况下应选中)。Admin
链接。Emit Test
按钮(右上角导航)您将在控制台中看到两个组件都收到了 Event,即使两个组件都不应该是“ Active ”!!!
1)这是设计使然吗?
2) 如何知道当前组件是否是活动组件?
根据我的理解,EventEmitter/Subject 可以被订阅多次,但是当取消订阅时你不能使用它/订阅它 AFAIK。
我的解决方案是:
1)在服务中,我结束了以下操作:
get(){
if (!this.testEvent.closed) {
this.testEvent.complete();
this.testEvent.unsubscribe();
}
this.testEvent= new EventEmitter<void>();
return this.testEvent;
}
}
Run Code Online (Sandbox Code Playgroud)
2)在我订阅事件的每个组件中,我添加了 ngDestroy 并取消订阅事件:
ngOnDestroy() {
this.testEvent.unsubscribe();
}
Run Code Online (Sandbox Code Playgroud)