小编Dom*_*omo的帖子

使用身份验证在 Blazor 服务器端下载文件

我有一个使用默认授权和身份验证的 Blazor 服务器端 Web 应用程序。

app.UseAuthentication()
app.UseAuthorization()
Run Code Online (Sandbox Code Playgroud)

我可以保护我的页面

@attribute [Authorize]
Run Code Online (Sandbox Code Playgroud)

我有一个匿名访问的登录页面进行身份验证。这工作正常。

现在我需要一种方法让用户从这个授权页面下载文件。令人惊讶的是,我还没有找到任何直接的方法来做到这一点。

一种解决方法是使用文件名作为路径参数构建 API 控制器,并为用户提供指向它的链接。

[Route("api/[controller]")]
public class FileController{

  [HttpGet("download/{filename}")]
  public async Task<IActionResult> Download([FromRoute] string filename){
  
    //Do some checks and get file from Filesystem

    return file;
  }
}
Run Code Online (Sandbox Code Playgroud)

在 .razor 文件中

<a href="@CalculateDownloadLink("file.txt")" target="_blank"></a>

private string CalculateDownloadLink(string filename){
  return $"{NavigationManager.BaseUri}/api/file/download/{filename}"
}
Run Code Online (Sandbox Code Playgroud)

这是一个简化版。实际上,文件名是通用的。这也有效。

现在我想向 API 控制器添加身份验证,因为我不想让任何人猜测文件名。但我不知道怎么做。

当然 [Authorize] 属性不起作用,因为代码在电路范围之外。我不知道如何使用任何内置授权来完成这项工作。

有没有更好的方法从 Blazor 应用程序下载文件?

asp.net-core blazor-server-side

6
推荐指数
1
解决办法
669
查看次数

一个应用程序中的Eureka Server和Spring Boot Admin

我尝试在一个应用程序中运行Eureka Server和Spring Boot Admin Server(SBA Docu说这是可能的)。Eureka可以按预期工作,但Admin App仍显示零个“应用程序”。

Spring Boot版本2.0.3,Spring Boot管理版本2.0.1

尤里卡和SBA服务器

@SpringBootApplication
@EnableEurekaServer
@EnableDiscoveryClient
@EnableAdminServer
class KotlintestApplication
fun main(args: Array<String>) {
    SpringApplication.run(KotlintestApplication::class.java, *args)
}
Run Code Online (Sandbox Code Playgroud)

服务器bootstrap.yml

spring:
      application:
        name: server
Run Code Online (Sandbox Code Playgroud)

服务器application.yml

spring:
  boot:
   admin:
     context-path: /admin

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8080/eureka
Run Code Online (Sandbox Code Playgroud)

客户

@EnableDiscoveryClient
@SpringBootApplication
class KotlintestApplication

fun main(args: Array<String>) {
    SpringApplication.run(KotlintestApplication::class.java, *args)
}

@Configuration
class SecurityPermitAllConfig : WebSecurityConfigurerAdapter() {
    @Throws(Exception::class)
    override fun configure(http: HttpSecurity) {
        http.authorizeRequests().anyRequest().permitAll()
                .and().csrf().disable()
    }
}
Run Code Online (Sandbox Code Playgroud)

客户端bootstrap.yml

 spring:
      application:
        name: client …
Run Code Online (Sandbox Code Playgroud)

spring-boot netflix-eureka spring-boot-admin

5
推荐指数
1
解决办法
2330
查看次数