22-Consent 确认逻辑实现

1-定义一个从前台传到后台的viewModel

namespace MvcCookieAuthSample.Models
{
    public class InputConsentViewModel
    {
        public string Button { get; set; }

        public IEnumerable<string> ScopesConsented { get; set; }

        public bool RemeberConsent { get; set; }

        public string ReturnUrl { get; set; }

    }
}

2-确定逻辑实现

        [HttpPost]
        public async Task<IActionResult> Index(InputConsentViewModel viewModel)
        {
            ConsentResponse consentResponse = null;
            if (viewModel.Button == "no")
            {
                consentResponse = ConsentResponse.Denied;
            }
            else if (viewModel.Button == "yes")
            {
                if(viewModel.ScopesConsented!=null && viewModel.ScopesConsented.Any())
                {
                    consentResponse = new ConsentResponse {
                         ScopesConsented=viewModel.ScopesConsented,
                         RememberConsent = viewModel.RemeberConsent
                    };
                  
                }
            }
            if (consentResponse != null)
            {
                var request = await _identityServerInteractionService.GetAuthorizationContextAsync(viewModel.ReturnUrl);
                await _identityServerInteractionService.GrantConsentAsync(request, consentResponse);
                return  Redirect(viewModel.ReturnUrl);
            }

            return View();
        }

3-前台consent/index.cshtml

@using MvcCookieAuthSample.ViewModel;
@model ConsentViewModel

<div class="row page-header">
    <div class="col-sm-10">
        @if (string.IsNullOrWhiteSpace(Model.ClientLogUrl))
        {
            <div>
                <img src="@Model.ClientLogUrl" />
            </div>
        }
        <h1>
            @Model.ClientName
            <small>希望使用你的账号</small>
        </h1>
    </div>


</div>

<div class="row">
    <form method="post" asp-route-ReturnUrl="@Model.ReturnUrl">
        @if (Model.IdentityScopes.Any())
        {

            <div class="panel">
                <div class="panel-heading">
                    <span class="glyphicon glyphicon-tasks"></span>
                    identity应用权限
                </div>

                <ul class="list-group">

                    @foreach (var scope in Model.IdentityScopes)
                    {                      
                        @await Html.PartialAsync("_ScopeListItem", scope);
                    }
                </ul>
            </div>

        }

        @if (Model.ResourceScopes.Any())
        {
            <div class="panel">
                <div class="panel-heading">
                    <span class="glyphicon glyphicon-tasks"></span>
                    resource应用权限
                </div>

                <ul class="list-group">
                    @foreach (var scope in Model.ResourceScopes)
                    {
                        await Html.PartialAsync("_ScopeListItem", scope);
                    }
                </ul>
            </div>

        }
        <div>
            <button value="yes" name="button" class="btn btn-primary"  autofocus>同意</button>
            <button value="no" name="button" >取消</button>

            @if (!string.IsNullOrEmpty(Model.ClientUrl))
            {
                <a href="@Model.ClientUrl" class="pull-right btn btn-default">
                    <span class="glyphicon glyphicon-info-sign" ></span>
                    <strong>@Model.ClientUrl</strong>
                </a>

            }
        </div>
    </form>
</div>

4 _scopeListItem.cshtml

@*
    For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*@
@using MvcCookieAuthSample.ViewModel;
@model ScopeViewModel

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

        }

        <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>

 

posted @ 2018-08-28 22:03  深圳丶追  阅读(214)  评论(0编辑  收藏  举报