> For the complete documentation index, see [llms.txt](https://mingsonzheng.gitbook.io/aspnetcore-distributed-project-combat/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mingsonzheng.gitbook.io/aspnetcore-distributed-project-combat/consent-shi-tu-zhi-zuo.md).

# Consent视图制作

#### 任务19：Consent视图制作 <a href="#ren-wu-19consent-shi-tu-zhi-zuo" id="ren-wu-19consent-shi-tu-zhi-zuo"></a>

按照上一节 Consent 的思路

在 mvcCookieAuthSample 项目的 Controllers 文件夹下新建一个 ConsentController

在 Views 文件夹下新建一个 Consent 文件夹，然后在该文件夹下新建一个 Index 视图

在 ViewModels 文件夹下新建一个 ConsentViewModel

在 ViewModels 文件夹下新建一个 ScopeViewModel

**ScopeViewModel.cs**

```
namespace mvcCookieAuthSample.ViewModels
{
    public class ScopeViewModel
    {
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public string Description { get; set; }
        public string Emphasize { get; set; }
        public string Required { get; set; }
        public bool Checked { get; set; }
    }
}
```

这个定义来自于 IdentityServer4 官方文档中 Identity Resource 和 API Resource 的模型\
<https://identityserver4.readthedocs.io/en/latest/reference/identity_resource.html>

![](/files/byraM9AKxS1obRBpv6xB)

**ConsentViewModel.cs**

```
namespace mvcCookieAuthSample.ViewModels
{
    public class ConsentViewModel
    {
        public string ClientId { get; set; }
        public string ClientName { get; set; }
        public string ClientLogoUrl { get; set; }

        /// <summary>
        /// 是否允许第二次登录不需要再次授权
        /// </summary>
        public bool AllowRemeberConsent { get; set; }

        // 对两种用户分别做出选择
        public IEnumerable<ScopeViewModel> IdentityScopes { get; set; }
        public IEnumerable<ScopeViewModel> ResourceScopes { get; set; }
    }
}
```

接下来需要把两个 ViewModel 的信息显示在 Index 里面，实现类似微博授权页面

![](/files/763DBtHz6Lu88sNGHBrE)

**\_ScopeListitem.cshtml**

```
@using mvcCookieAuthSample.ViewModels;
@model ScopeViewModel

<li>

</li>
```

**Index.cshtml**

```
@using mvcCookieAuthSample.ViewModels;
@model ConsentViewModel

<p>Consent Page</p>
<div class="row page-header">
    <div class="col-sm-10">
        @if (string.IsNullOrWhiteSpace(Model.ClientLogoUrl))
        {
            <div><img src="@Model.ClientLogoUrl"/></div>
        }

        <h1>
            @Model.ClientName
            <small>希望使用您的账户</small>
        </h1>
    </div>
</div>

<div class="row">
    <div class="col-sm-8">
        <form asp-action="Index">

            @if (Model.IdentityScopes.Any())
            {
                <ul class="list-group">
                    @foreach (var scope in Model.IdentityScopes)
                    {
                        @Html.Partial("_ScopeListitem", scope)
                    }
                </ul>
            }

            @if (Model.ResourceScopes.Any())
            {
                <ul class="list-group">
                    @foreach (var scope in Model.IdentityScopes)
                    {
                        @Html.Partial("_ScopeListitem", scope)
                    }
                </ul>
            }
        </form>
    </div>
</div>
```

上半部分展示图标和提示，下半部分分为三部分，分别列出 IdentityScopes 和 ResourceScopes，以及选择是否记住，最后实现授权
