我正在尝试在> wwwroot 我的.NET核心项目的文件夹中保护一个文件夹,其中包含以下代码 startup.cs :
app.Use(async (context, next) =>
{
if (context.Request.Path.StartsWithSegments("/Users"))
{
if (context.User.Identities.Any(identity => identity.IsAuthenticated))
{
if (context.Request.Path.StartsWithSegments("/Users") && context.Request.Path.Value.LastIndexOf("/") > 0)
{
var clientID = Guid.Parse(context.Request.Path.Value.Split("/"[0])[2]);
if (context.GetClientID() != clientID && !context.User.IsInRole("Administrator"))
await context.Authentication.ForbidAsync();
else
await next();
}
else
await next();
}
else
await context.Authentication.ChallengeAsync();
}
else
await next();
});
在Visual Studio和II Express中一切正常,但是当我将项目发布到IIS中时, context.user.indistities.any(Identity => Identity.isauthenticated(始终返回>false 。即使我使用 context.user.indity.isauthenticated 它返回false。
但是,这将用户重定向到登录页面,尽管它们登录了,并且当用户日志再次起作用时,它可以工作。
这是我的cookie身份验证的代码:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = CoreApplicationInfo.AuthenticationScheme,
LoginPath = new PathString("/login"),
CookieName = CoreApplicationInfo.ApplicationCookie,
CookieHttpOnly = true,
ExpireTimeSpan = TimeSpan.FromDays(1),
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
任何人可以帮助我吗?
首先:
创建www文件夹并找到一个文件。
下一步:(用自己的代码指定路径(
[Authorize]
public IActionResult File()
{
return PhysicalFile(Path.Combine(environment.ContentRootPath, "www", "banner1.svg"), "image/svg+xml");
}
最好的问候。
https://medium.com/@tanaka_733/static-file-authorization-in-asp-net-core-mvc-26c1069073c1