【发布时间】:2014-01-04 13:48:24
【问题描述】:
我想对 VS 2013 中 ASP.NET 的默认单页应用程序模板进行修改,该模板目前使用不记名令牌身份验证。该示例使用 app.UseOAuthBearerTokens 创建令牌服务器和中间件,以验证同一应用中请求的令牌。
我想做的是保留它,但添加第二个应用程序(在 IIS 中绑定到同一个域,不同的路径 - 例如 /auth/* 用于身份验证服务器, /app1/* 用于应用程序)。对于第二个应用程序,我希望它接受第一个应用程序中的身份验证服务器发出的令牌。这怎么可能实现?我在 Startup.Auth.cs 中尝试了以下操作,只是从 UseOAuthBearerTokens 中的代码开始,但我得到了对任何具有 [Authorize] 属性的请求的 401 响应:
public partial class Startup
{
static Startup()
{
PublicClientId = "self";
UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>());
OAuthOptions = new OAuthAuthorizationServerOptions
{
//TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
//AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
//AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
AuthenticationType = "ExternalBearer",
AllowInsecureHttp = true,
};
}
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
public static Func<UserManager<IdentityUser>> UserManagerFactory { get; set; }
public static string PublicClientId { get; private set; }
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
//// Enable the application to use a cookie to store information for the signed in user
//// and to use a cookie to temporarily store information about a user logging in with a third party login provider
//app.UseCookieAuthentication(new CookieAuthenticationOptions());
//app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
OAuthBearerAuthenticationOptions bearerOptions = new OAuthBearerAuthenticationOptions();
bearerOptions.AccessTokenFormat = OAuthOptions.AccessTokenFormat;
bearerOptions.AccessTokenProvider = OAuthOptions.AccessTokenProvider;
bearerOptions.AuthenticationMode = OAuthOptions.AuthenticationMode;
bearerOptions.AuthenticationType = OAuthOptions.AuthenticationType;
bearerOptions.Description = OAuthOptions.Description;
bearerOptions.Provider = new CustomBearerAuthenticationProvider();
bearerOptions.SystemClock = OAuthOptions.SystemClock;
OAuthBearerAuthenticationExtensions.UseOAuthBearerAuthentication(app, bearerOptions);
}
}
public class CustomBearerAuthenticationProvider : OAuthBearerAuthenticationProvider
{
public override Task ValidateIdentity(OAuthValidateIdentityContext context)
{
var claims = context.Ticket.Identity.Claims;
if (claims.Count() == 0 || claims.Any(claim => claim.Issuer != "LOCAL AUTHORITY"))
context.Rejected();
return Task.FromResult<object>(null);
}
}
显然,我错过了第二个应用程序可以通过某种方式验证令牌来自第一个应用程序的部分。某种公共签名密钥?
这只是一个概念证明。
编辑:机器密钥建议在 POC 演示中运行良好,很高兴知道有支持其他关键场景的 AS 实现选项。
我能够使用此站点生成一个演示密钥(不用于生产): http://aspnetresources.com/tools/machineKey
并将结果放在 IIS 站点中托管的每个应用程序的 web.config 中的 <system.web> 元素下。我还必须在资源服务器的 Startup 类中删除一些特定于 AS 的配置选项。
【问题讨论】:
标签: asp.net oauth asp.net-web-api owin