C# OAuth中间件是否可能跨应用程序?
我想把我的项目分成三部分。还有单页应用程序、OAuth中间件,最后还有我的WebApi 用户应该能够登录,这就是我需要SPA和中间件的原因 如果中间件批准(用户获得授权),还可以执行从JavaScript到我的WebApi的AJAX请求 这就是中间件C# OAuth中间件是否可能跨应用程序?,c#,authentication,oauth,asp.net-web-api,C#,Authentication,Oauth,Asp.net Web Api,我想把我的项目分成三部分。还有单页应用程序、OAuth中间件,最后还有我的WebApi 用户应该能够登录,这就是我需要SPA和中间件的原因 如果中间件批准(用户获得授权),还可以执行从JavaScript到我的WebApi的AJAX请求 这就是中间件Startup.cs的配置方式: public void Configuration(IAppBuilder app) { app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); OAu
Startup.cs
的配置方式:
public void Configuration(IAppBuilder app)
{
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
OAuthAuthorizationServerOptions opt = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Security/Token"),
ApplicationCanDisplayErrors = true,
AllowInsecureHttp = true,
Provider = new OAuthAuthorizationServerProvider
{
OnValidateAuthorizeRequest = ValidateAuthorizeRequest,
OnGrantResourceOwnerCredentials = GrantResourceOwnerCredentials,
OnValidateClientAuthentication = ValidateClientAuthentication,
OnGrantRefreshToken = GrantRefreshToken,
},
RefreshTokenProvider = new AuthenticationTokenProvider
{
OnCreate = CreateRefreshToken,
OnReceive = ReceiveRefreshToken,
}
};
app.UseOAuthAuthorizationServer(opt);
OAuthBearerAuthenticationOptions _opt = new OAuthBearerAuthenticationOptions();
app.UseOAuthBearerAuthentication(_opt);
}
登录已经可以工作了(当与security/token通信时,我会得到一个200状态码),对放置在中间件中的资源的JavaScript调用也可以工作
我假设,如果这是可能的,我必须为我的WebApi配置另一个Owin Startup.cs
。
我所做的是:
public void ConfigureAuth(IAppBuilder app)
{
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
OAuthBearerAuthenticationOptions cOpt = new OAuthBearerAuthenticationOptions();
cOpt.Realm = "http://localhost:61188/Security/Token"; //auth server
cOpt.AuthenticationMode = AuthenticationMode.Active;
//cOpt.AccessTokenProvider = new AuthenticationTokenProvider();
cOpt.Provider = new OAuthBearerAuthenticationProvider
{
OnValidateIdentity = ValidateIdentity,
OnRequestToken = RequestToken
};
app.UseOAuthBearerAuthentication(cOpt);
}
您是否知道如何使对我的WebApi的请求成为可能
如果您需要更多我的代码,请询问;) 我们为BackOffice单页应用程序实现了此功能。我们为用户提供一个登录页面,用户可以在其中输入他们的凭据。我们将它们发送到身份验证服务器,该服务器重新运行请求、刷新令牌和过期日期。然后我们将其存储在cookie和内存中(当用户刷新页面而不重新输入凭据时,cookie用于对用户进行身份验证) 要从webapi请求数据,请求令牌将附加到
授权
头
$.ajax({
url: '/api/article',
headers: {
'Authorization': 'Bearer' + token
}
})
要续订请求令牌,将计时器设置为过期日期减去几秒钟
setTimeout(function() {
$.ajax({
method: 'post',
url: '/token',
data: {
refresh_token: refreshToken,
grant_type: 'refresh_token'
},
headers: {
'Authorization': 'Bearer' + token
},
success: function() {
// store new token
}
});
}, new Date(expireDate) - (new Date()) - 120000);
将
Microsoft.Owin.Host.SystemWeb
引用添加到我的项目解决了这个问题。使用$.ajax
时,我仍然收到未经授权的-响应,但angulars资源和http
服务工作正常
我还可以将启动操作中的资源服务器限制为:
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
OAuthBearerAuthenticationOptions opt = new OAuthBearerAuthenticationOptions();
app.UseOAuthBearerAuthentication(opt);
是的,我们也是这样做的。正如我所说,登录已经起作用了,这意味着:用户可以登录并获得一个承载令牌。它将存储在客户端,并随每个Api请求一起发送。我的问题是如何配置Api。即使Api获得了正确的令牌,它也会回答401:Unauthorized
。我的猜测是,这是因为由于Owin配置错误,Api无法与Auth服务器进行通信。
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
OAuthBearerAuthenticationOptions opt = new OAuthBearerAuthenticationOptions();
app.UseOAuthBearerAuthentication(opt);