Web API 2 OWIN承载令牌认证 - AccessTokenFormat null?
默认/令牌端点工作正常,我可以从那里获得令牌,但我需要在票证上使用AccessTokenFormat.Protect方法为externalLogin生成accessToken.
基本上我的实现与这个实现几乎相同,我遇到了同样的问题,即AccessTokenFormat为null.从文档中可以看出:
用于保护访问令牌中包含的信息的数据格式.如果应用程序未提供,则默认数据保护提供程序取决于主机服务器.IIS上的SystemWeb主机将使用ASP.NET机器密钥数据保护,而HttpListener和其他自托管服务器将使用DPAPI数据保护.如果分配了不同的访问令牌提供程序或格式,则必须将兼容的实例分配给资源服务器的OAuthBearerAuthenticationOptions.AccessTokenProvider或OAuthBearerAuthenticationOptions.AccessTokenFormat属性.
在我看来,如果未分配AccessTokenFormat,主机将为其提供基本实现.但我不认为它在这里有效.有没有办法找到ISecureDataFormatAccessTokenFormat的默认实现并手动将其分配给变量?
或者有没有人有其他想法如何解决这个问题?
更新:我获取katana的源代码并找到OAuthAuthorizationServerMiddleware类,从源代码我可以看到以下代码:
if (Options.AccessTokenFormat == null)
{
IDataProtector dataProtecter = app.CreateDataProtector(
typeof(OAuthAuthorizationServerMiddleware).Namespace,
"Access_Token", "v1");
Options.AccessTokenFormat = new TicketDataFormat(dataProtecter);
}
Run Code Online (Sandbox Code Playgroud)
在我的Startup.Auth中,这是我的代码:
static Startup()
{
PublicClientId = "self";
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
OAuthOptions = new OAuthAuthorizationServerOptions()
{
TokenEndpointPath = new PathString("/Token"),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
OAuthBearerOptions.AccessTokenFormat = OAuthOptions.AccessTokenFormat;
OAuthBearerOptions.AccessTokenProvider …Run Code Online (Sandbox Code Playgroud) 旋转的回文类似于"1234321","3432112".天真的方法将把字符串切成不同的部分并将它们连接起来,看看字符串是否是回文.这将花费O(n ^ 2),因为有n个切割,并且对于每个切割,我们需要O(n)来检查该串是否是回文.我想知道是否有比这更好的解决方案.我想是的,请指教.
谢谢!
有谁知道如何从动作过滤器中的属性路由获取路由名称?
例如,我有一个控制器和属性路由,如下所示:
[HttpGet]
[CustomActionAttribute]
[Route("~/index", Name="IndexPage")]
public async Task<ActionResult> Index()
{
//Controller logic
}
Run Code Online (Sandbox Code Playgroud)
是否可以在CustomActionAttribute中获取路由名称?
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//Get the current route name here
}
Run Code Online (Sandbox Code Playgroud)