LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

ASP.NET Core のセキュリティ機能

Posted at
1 / 19
  • ASP.NET Core 組み込み
  • .NET 組み込みの認可
  • セキュリティヘッダー
  • おまけ

ASP.NET Core 組み込み


ASP.NET Authentication Core

認証はそろってるので活用しよう!

  • パスワード認証 (実装は ASP.NET Identity Core)
  • SNS 認証
  • OAuth 2.0 / OpenID Connect
  • カスタム認証
  • メールを送ったり、二要素認証にも確か対応してる

ASP.NET Core 2.0 Authentication


パスワード認証


ASP.NET Authorization Core

Authorize(Roles = "Administrators")
public IActionResult Get() {}

CSRF

<form action="/" method="post">
    @Html.AntiForgeryToken()
</form>
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Save(DataModel model) {}

CORS

// 上記サイトから一部引用
public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("AllowSpecificOrigin", builder => builder
            .WithOrigins("http://example.com"));
    });
}

public void Configure(IApplicationBuilder app)
{
    // ...

    // Shows UseCors with named policy.
    app.UseCors("AllowSpecificOrigin");

    // ...
}

Secret Manager

  • 開発時に使える API トークンとかを書いておく場所
  • %APPDATA%\Microsoft\UserSecrets\<user_secrets_id>\secrets.json or ~/.microsoft/usersecrets/<user_secrets_id>/secrets.json
  • 暗号化されているわけではない
  • appsettings.json と同じ書き方
<!-- csproj -->
<PropertyGroup>
  <TargetFramework>netcoreapp2.1</TargetFramework>
  <UserSecretsId>my-app-name</UserSecretsId>
</PropertyGroup>

.NET 組み込み


[PrincipalPermission(SecurityAction.Demand, Role = "Administrators")]
public void Method() {}

  • ロールベースのセキュリティ
  • .NET の Identity を使うとこれに限らず便利なメソッドがあるので活用しよう
  • ASP.NET Authorization Core もこれに設定してくれる
  • ASP.NET Core のと違って、Controller 以外でも使える (ただし、例外がスローされるので 5xx になる)
var identity = new GenericIdentity("太郎");
var principal = new GenericPrincipal(identity, new[] {
  "Administrators",
  "Managers"
});

Thread.CurrentPrincipal = principal;

セキュリティヘッダー (外部ライブラリ)


こういうの (知りたい方はググってね!)

  • X-Frame-Options
  • X-Content-Type-Options
  • Strict-Transport-Security
  • X-XSS-Protection
  • Content-Security-Policy (さっきやったけど)
  • OWASP Secure Headers Project


おまけ


リソースごとの認可

  • 管理者しかアクセスできないは簡単なんだけどね...
  • 自身しかアクセスできないは自力で実装しないといけない
  • これがめんどくさいのよ
// ダメな例
public IActionResult Update(ToDo item)
{
  if (item.OwnerId 1= User.Identity.Name)
    return NotFound();

  // ここにデータベースに保存する処理
}

XSS

  • Razor はデフォルトが XSS にたいして安全なので安心
  • script 要素や一部の属性はさらなる対策が必要
<!-- これが危険なのはみんなわかるよね? -->
<script>
  @address
</script>
<!-- これも危険なので注意しようね! -->
<img src="@profileImage">

<!--
  profileImage 変数の値が
  "javascript:alert(document.cookie)"
  だったら...
-->

終わり

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up