OAuth中间件交叉应用可能吗?

时间:2014-07-08 14:51:29

标签: 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);

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

登录已经有效(在与安全/令牌通信时获得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请求成为可能?

如果您需要更多代码,请询问;)

2 个答案:

答案 0 :(得分:0)

我们为BackOffice单页面应用程序实现了这一功能。我们为用户提供了一个登录页面,用于输入凭据。我们将它们发送到验证服务器,该服务器返回请求,刷新令牌和过期日期。然后我们将它们存储在cookie和内存中(该cookie用于在刷新页面时对用户进行身份验证,而无需重新输入凭据)。

var requestToken, refreshToken, expireDate;

$.ajax({
    method: 'post',
    url: '/api/token',
    data: {
        username: 'John Doe',
        password: 'secret',
        grant_type: 'password'
    },
    success: function(res) {
        requestToken = res.requestToken;
        refreshToken = res.refreshToken;
        expireDate = res.expireDate;
    }
})

要从webapi请求数据,请求令牌将附加到Authorization标题。

$.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); 

答案 1 :(得分:0)

Microsoft.Owin.Host.SystemWeb引用添加到我的项目中解决了这个问题。我在使用Unauthorized时仍然会收到$.ajax - 回复,但是angulars resourcehttp服务正常运行。

我还可以将启动操作的资源服务器限制为:

app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
OAuthBearerAuthenticationOptions opt = new OAuthBearerAuthenticationOptions();
app.UseOAuthBearerAuthentication(opt);