通过代码使用SSO在多台计算机上授权WebAPI

时间:2014-09-12 11:54:17

标签: authentication asp.net-web-api oauth active-directory owin

我有一个Web API项目,它只根据角色提供对授权用户的访问权限。我们计划在不久的将来扩展到多个这样的API项目,这些项目将使用相同的授权令牌。我正在做的是使用一个单独的项目来验证用户(使用Facebook,Google或ActiveDirectory或任何其他身份验证提供程序),然后生成一个由所有API服务器使用的授权令牌。为了解密令牌,我在所有应用程序中通过web.config共享机器密钥。这很好用。现在我想每隔一段时间更改一次MachineKey,并在使用授权令牌的所有应用程序中共享它。

  1. 如何在运行时更新machineKey 应用程序?
  2. 有没有一种程序化的方法来实现同样的目标?
  3. 我在博客中读过,更新machineKey并不是一个好习惯。如果是这样,我如何完全避免使用机器密钥并创建一个密钥不是静态的系统?
  4. 我的想法是将授权项目与我的WebAPI项目分开,这样我就不会在所有WebAPI项目中实现和认证系统。任何指针都会非常有用。

3 个答案:

答案 0 :(得分:4)

Extended the implementation显示在以下stackoverflow答案中,以满足您的要求。我将在几天内更新代码,了解如何实现这一目标。

答案 1 :(得分:0)

您可以让API请求Authorization-Service来确认来自客户端的令牌,并在同一请求中获取从服务到API的ID数据。为了减少Auth-Service的流量,它可以将具有过期日期的短期访问令牌分发给API,需要在几分钟后刷新,或者如果它们很关键,可以直接从Auth-Service呼叫API。

你的系统对我来说听起来很像OAuth计划。除非您计划使用移动设备等其他用例访问您的服务,否则我不建议您完全符合要求,但您可以了解他们如何做到这一点并实施与您的需求相关的内容。

答案 2 :(得分:0)

根据Rajesh的建议,我相信我所寻找的是对他的联系的微不足道的提升。 基本上我需要创建一个在Protect方法中设置我的键的函数。

private IDataProtector protector;

public string Protect(AuthenticationTicket ticket)
{
    protector = new AesDataProtectorProvider(functionGenerateKey()); 
    //functionGenerateKey is a function that generates a new key and informs 
    //the subscribers to avail new key
    var ticketData = this.serializer.Serialize(ticket);
    var protectedData = this.protector.Protect(ticketData);
    var protectedString = this.encoder.Encode(protectedData);
    return protectedString;
}

public SecureTokenFormatter(string key)
{
    this.serializer = new TicketSerializer();
    //this.protector = new AesDataProtectorProvider(key); -> Remove the initialization of this protector as I am doing it in the above function. 
    this.encoder = TextEncodings.Base64Url;
}

其余的实现在Barguast

编写的代码中非常标准