0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ASP.NET Core Identity ログイン画面のカスタマイズ

Last updated at Posted at 2020-04-18

IdentityでMVCにログイン画面を追加出来ましたが、ユーザーIDがメールアドレス(user01@raspberry.pi)なのでユーザー名(user01)に変更したい。
https://qiita.com/namikitakeo/items/0de598b8e43eb5b1ff94

Identityをスキャフォールディングします。
https://www.slideshare.net/YutaMatsumura/aspnet-core-2x-identity

$ dotnet aspnet-codegenerator identity -dc myop.Models.ApplicationDbContext

Input.EmailをInput.UserNameに変更します。あわせてLogin.cshtml.csも修正が必要です。

Areas/Identity/Pages/Account/Login.cshtml
@page
@model LoginModel

@{
    ViewData["Title"] = "Log in";
}

<h1>@ViewData["Title"]</h1>
<div class="row">
    <div class="col-md-4">
        <section>
            <form id="account" method="post">
                <h4>Use a local account to log in.</h4>
                <hr />
                <div asp-validation-summary="All" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="Input.UserName"></label>
                    <input asp-for="Input.UserName" class="form-control" />
                    <span asp-validation-for="Input.UserName" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Input.Password"></label>
                    <input asp-for="Input.Password" class="form-control" />
                    <span asp-validation-for="Input.Password" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <div class="checkbox">
                        <label asp-for="Input.RememberMe">
                            <input asp-for="Input.RememberMe" />
                            @Html.DisplayNameFor(m => m.Input.RememberMe)
                        </label>
                    </div>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary">Log in</button>
                </div>
                <div class="form-group">
                    <!-- p>
                        <a id="forgot-password" asp-page="./ForgotPassword">Forgot your password?</a>
                    </p -->
                    <p>
                        <a asp-page="./Register" asp-route-returnUrl="@Model.ReturnUrl">Register as a new user</a>
                    </p>
                    <!-- p>
                        <a id="resend-confirmation" asp-page="./ResendEmailConfirmation">Resend email confirmation</a>
                    </p -->
                </div>
            </form>
        </section>
    </div>
    <div class="col-md-6 col-md-offset-2">
        <section>
            <h4>Use another service to log in.</h4>
            <hr />
            @{
                if ((Model.ExternalLogins?.Count ?? 0) == 0)
                {
                    <div>
                        <p>
                            There are no external authentication services configured. See <a href="https://go.microsoft.com/fwlink/?LinkID=532715">this article</a>
                            for details on setting up this ASP.NET application to support logging in via external services.
                        </p>
                    </div>
                }
                else
                {
                    <form id="external-account" asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal">
                        <div>
                            <p>
                                @foreach (var provider in Model.ExternalLogins)
                                {
                                    <button type="submit" class="btn btn-primary" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>
                                }
                            </p>
                        </div>
                    </form>
                }
            }
        </section>
    </div>
</div>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

Input.EmailをInput.UserNameに変更します。あわせてRegister.cshtml.csも修正が必要です。

Areas/Identity/Pages/Account/Register.cshtml
@page
@model RegisterModel
@{
    ViewData["Title"] = "Register";
}

<h1>@ViewData["Title"]</h1>

<div class="row">
    <div class="col-md-4">
        <form asp-route-returnUrl="@Model.ReturnUrl" method="post">
            <h4>Create a new account.</h4>
            <hr />
            <div asp-validation-summary="All" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Input.UserName"></label>
                <input asp-for="Input.UserName" class="form-control" />
                <span asp-validation-for="Input.UserName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Input.Password"></label>
                <input asp-for="Input.Password" class="form-control" />
                <span asp-validation-for="Input.Password" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Input.ConfirmPassword"></label>
                <input asp-for="Input.ConfirmPassword" class="form-control" />
                <span asp-validation-for="Input.ConfirmPassword" class="text-danger"></span>
            </div>
            <button type="submit" class="btn btn-primary">Register</button>
        </form>
    </div>
    <div class="col-md-6 col-md-offset-2">
        <section>
            <h4>Use another service to register.</h4>
            <hr />
            @{
                if ((Model.ExternalLogins?.Count ?? 0) == 0)
                {
                    <div>
                        <p>
                            There are no external authentication services configured. See <a href="https://go.microsoft.com/fwlink/?LinkID=532715">this article</a>
                            for details on setting up this ASP.NET application to support logging in via external services.
                        </p>
                    </div>
                }
                else
                {
                    <form id="external-account" asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal">
                        <div>
                            <p>
                                @foreach (var provider in Model.ExternalLogins)
                                {
                                    <button type="submit" class="btn btn-primary" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>
                                }
                            </p>
                        </div>
                    </form>
                }
            }
        </section>
    </div>
</div>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

SQLite3 CLIで確認してみます。
https://qiita.com/aki3061/items/f6450bdf3675418f0ef0

% sqlite3 app.db 
SQLite version 3.28.0 2019-04-15 14:49:49
Enter ".help" for usage hints.

sqlite> .tables
AspNetRoleClaims       AspNetUserRoles        Codes                
AspNetRoles            AspNetUserTokens       Tokens               
AspNetUserClaims       AspNetUsers            __EFMigrationsHistory
AspNetUserLogins       Clients              

sqlite> select * from AspNetUsers;
a225ff8f-b5f0-4642-b121-b83a5a24ccd6|0|f679b63a-a20b-43a7-bce7-732b41ad1f1f||0|1|||USER01|AQAAAAEAACcQAAAAENSroFqdRMCx2KXhdL6AYRCJZv8gI5uvJN3pSvV3nIYBS/LQupOnPunWxFgW+JM/LQ==||0|TODFM4APPYGCTS6HUA5CJDHBLLCELSAQ|0|user01
2b916bd3-6d90-42b3-a429-ddeda7fa47fa|0|b299e030-f386-40b4-a152-7aeee466b57b||0|1|||USER02|AQAAAAEAACcQAAAAEK/K4m/sNck9Oz8GcOB3btJlY7C4k8zT/57XXkceL3FpbFXSoOZEMt91aLfI05aJOQ==||0|OUYX2V55TNGXSL2SD2OJMRS7JKFCVACT|0|user02
23ad1a48-fb52-419a-80c2-399b4ff0dc39|0|a88b87f8-fd92-4759-bc1f-f0d15f6740f4||0|1|||USER03|AQAAAAEAACcQAAAAED1w8pPzc64DbCccLZBk5yOUmp9TVYsv3sNofHgMWu0LMCb6k6GbWjm5ytSSZUJQ5A==||0|YVGFWYXXIQO5XTUMQBLUECIERPRAA5T2|0|user03

sqlite> .exit
0
3
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
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?