gpt4 book ai didi

angularjs - webapi owin 使用 token 和 cookie

转载 作者:行者123 更新时间:2023-12-02 23:04:31 25 4
gpt4 key购买 nike

我的 Web 应用程序中有两个主要项目:

  1. WebApi 项目作为后端,为 Web 项目提供身份验证和授权服务,使用 OWIN 2 和不记名 token 。
  2. Web 项目使用 Angularjs。

Web 项目按预期工作(身份验证和授权正在工作)

方法:将 token 存储到本地存储,并在每次请求时使用拦截器发送它。

现在我想向 WebApi 项目添加身份验证和授权,该项目将为 Hangfire、Elmah 和帮助页面等其他模块提供服务。我添加了相同的登录逻辑,该逻辑有效(授权),然后重定向到有效的仪表板页面(使用 Angularjs)。

但是转到任何其他页面(提到的模块之一)都不起作用。如果不起作用:Owin 上下文中的用户始终为 null/空。(参见代码)

根据我的理解,我需要以某种方式在每个请求中发送 token ,但这在这里不会发生。

问题:

  1. 我怎样才能实现这一点(发送/获取 token )?

    如果cookie是唯一/更好的方法↴

  2. 如何集成项目 1 的 cookie 和项目 2 的 token ?(尝试使用 cookie,但似乎我做错了,或者它是否与不记名 token 同时工作?)

代码:

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();
});
}

我尝试用于测试目的:

public class HFAuthorizationFilter : Hangfire.Dashboard.IAuthorizationFilter
{
public bool Authorize(IDictionary<string, object> owinEnvironment)
{
var context = new OwinContext(owinEnvironment);

if (context.Authentication.User == null)
return false;//Always null

return context.Authentication.User.HasClaim(ClaimTypes.Role, "SuperAdmin")
|| context.Authentication.User.HasClaim(ClaimTypes.Role, "Admin");
}
}

并在配置中:

app.UseHangfire(hangfireConfig =>
{
hangfireConfig.UseAuthorizationFilters(
new HFAuthorizationFilter()
);

hangfireConfig.UseSqlServerStorage("Context");
hangfireConfig.UseServer();
});

潜在的重复: Passing and verifying the OWIN Bearer token in Query String in WebAPI

最佳答案

如果我理解正确,您希望在一个 api 中实现 token 生成,并在其他 api 中使用相同的 token 。如果是这种情况,那么您需要主 api 作为 token 生成器,并需要子或依赖 api 来使用 token 。请查找 oauth 的主 API 和子 API 配置主API配置:

public void ConfigureOAuth(IAppBuilder app)
{
//configure OAuth using owin framework
var oAuthOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/api/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromHours(2),
Provider = new KatanaAuthorizationServerProvider()

};
app.UseOAuthAuthorizationServer(oAuthOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}

子 API 配置:

public void ConfigureAuth(IAppBuilder app)
{
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}

关于angularjs - webapi owin 使用 token 和 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28135322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com