ASP.NET Core分布式项目实战(运行Consent Page)--学习笔记

任务21:运行Consent Page

修改 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="[email protected]"
               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="[email protected]">@Model.Description</label>
        </div>
    }
</li>

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

如果界面出现乱码,可将文件 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))

再次启动程序,显示如下:

课程链接

http://video.jessetalk.cn/course/explore

知识共享许可协议

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。

如有任何疑问,请与我联系 ([email protected]) 。

猜你喜欢

转载自www.cnblogs.com/MingsonZheng/p/12892456.html