# 运行Consent Page

#### 任务21：运行Consent Page <a href="#ren-wu-21-yun-hang-consentpage" id="ren-wu-21-yun-hang-consentpage"></a>

修改 Config.cs 中的 RequireConsent 为 true，这样登录的时候就会跳转到 Consent 页面

**修改 ConsentController 的 Index 为异步**

```
[HttpGet]
public async Task<IActionResult> Index(string returnUrl)
{
    var model = await BuildConsentViewModel(returnUrl);

    if (model == null)
    {

    }

    return View(model);
}
```

**构造函数改为 public**

```
public ConsentController
```

**Index.cshtml 添加用户信息和 icon**

```
@if (Model.IdentityScopes.Any())
{
    <div>
        <span class="glyphicon glyphicon-user"></span>
        用户信息
    </div>
    <ul class="list-group">
        @foreach (var scope in Model.IdentityScopes)
        {
            @Html.Partial("_ScopeListitem", scope)
        }
    </ul>
}
```

**\_ScopeListitem.cshtml**

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

<li>
    <label>
        <input type="checkbox"
               name="ScopesConsented"
               id="scopes_@Model.Name"
               value="@Model.Name"
               checked="@Model.Checked"
               disabled="@Model.Required"/>

        <strong>@Model.Name</strong>
        @if (Model.Emphasize)
        {
            <span class="glyphicon glyphicon-exclamation-sign"></span>
        }
    </label>
    @if (string.IsNullOrEmpty(Model.Description))
    {
        <div>
            <label for="scopes_@Model.Name">@Model.Description</label>
        </div>
    }
</li>
```

启动服务端 mvcCookieAuthSample，再启动客户端 MvcClient，登录之后跳转到 Consent 页面

![](https://2301491750-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLZEPhWwFW7gQU2ww1DAC%2Fuploads%2FxxH9og65rTYTI77A3r6G%2F126.jpg?alt=media\&token=4c6a6910-f876-468a-b88b-356a6d08ecf8)

如果界面出现乱码，可将文件 Index.cshtml 与 \_ScopeListitem.cshtml 另存为 UTF-8 编码

**在 Config 的 GetClients 中补充客户端信息**

```
public static IEnumerable<Client> GetClients()
{
    return new List<Client>
    {
        new Client()
        {
            ClientId = "client",
            ClientName = "Client",
            ClientUri = "http://localhost:5001",
            LogoUri = "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQe9J82GNBVtbc0Co3IV63l4mQgRlMdn4lQI7cDmICHjA4VmFuv&usqp=CAU",
            AllowRememberConsent = true,
            AllowedGrantTypes = GrantTypes.Implicit,// 隐式模式
            ClientSecrets =
            {
                new Secret("secret".Sha256())
            },

            RequireConsent = true,
            RedirectUris = { "http://localhost:5001/signin-oidc" },
            PostLogoutRedirectUris = { "http://localhost:5001/signout-callback-oidc" },

            //AllowedScopes = {"api"},
            AllowedScopes =
            {
                IdentityServerConstants.StandardScopes.Profile,
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Email,
            }
        }
    };
}
```

修改 Index.cshtml，不然看不到图片

```
@if (!string.IsNullOrWhiteSpace(Model.ClientLogoUrl))
```

再次启动程序，显示如下：

![](https://2301491750-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLZEPhWwFW7gQU2ww1DAC%2Fuploads%2FKVICSKpw7j1AkA8VqTNz%2F127.jpg?alt=media\&token=66cd451c-e8bb-4e10-91f9-5765a8d9cc31)
