简体   繁体   English

ASP.NET MVC-如何显示返回到javascript的“ ActionResult”视图?

[英]ASP.NET MVC - How to display an “ActionResult” view that is returned to javascript?

I'm using DotNetOpenAuth with a custom login provider to sign users into my site. 我将DotNetOpenAuth与自定义登录提供程序结合使用,以将用户登录到我的网站。 I have most of the code working, but am having an issue returning the view and displaying it so users can log into the provider. 我的大部分代码都在工作,但是返回视图并显示它时出现问题,以便用户可以登录到提供程序。 Basically, the action is returning an actionresult which is the login page on the specific provider back to the calling javascript. 基本上,该操作将一个actionresult返回到调用javascript,该结果是特定提供程序上的登录页面。 I expected this, as I need to get the login results back to the javascript. 我期望如此,因为我需要将登录结果返回到javascript。 Basically, what happens is I get some error about allowing access then three null boxes (look in the return of the call) and then nothing. 基本上,发生的事情是我在允许访问时出现一些错误,然后出现了三个空框(看回电话),然后什么也没有。 I need it to goto the provider site and log in. 我需要它去提供者站点并登录。

Data Example: url = https://www.google.com/accounts/o8/id , requiresUserName = false, username = test (assume it was entered into the textbox). 数据示例:url = https://www.google.com/accounts/o8/id,requireUserName = false,username = test(假设已在文本框中输入)。

Below is the code: 下面是代码:

JavaScript: JavaScript:

function ProviderSignOn(url, requiresUserName) {
    var username = $("#openUserNameText").val();
    if ((requiresUserName == 'True') && (username == '')) {
        clickedProviderUrl = url;
        $('#openUserNameDiv').show('fast');
    }
    else {
        $('#openUserNameDiv').hide('fast');
        $("#openUserNameText").val('');
        $.ajax({
            type: "POST",
            url: "/Account/ProviderSignOn",
            data: "provider=" + url + "&username=" + username,
            success: function (data) {
                alert(data.Error);
                alert(data.Message);
                alert(data.Identifier);
            },
            failure: function (data) {
                alert(data.Error);
                alert(data.Message);
                alert(data.Identifier);
            }
        });
    }
}

Controller Action: 控制器动作:

public ActionResult ProviderSignOn(string provider, string username)
    {
        string providerUrl = provider.Replace("{username}", username);
        bool error = true;
        string message = String.Empty;
        string identifier = String.Empty;

        var response = _openId.GetResponse();

        if (response == null)
        {
            Identifier id;

            if (Identifier.TryParse(providerUrl, out id))
            {
                try
                {
                    return _openId.CreateRequest(providerUrl).RedirectingResponse.AsActionResult();
                }
                catch (ProtocolException ex)
                {
                    throw ex;
                }
            }
            else
            {
                error = true;
                message = "Invalid";
                identifier = String.Empty;
            }
        }
        else
        {
            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:
                    error = false;
                    message = "Success";
                    identifier = response.ClaimedIdentifier;
                    break;
                case AuthenticationStatus.Canceled:
                    error = true;
                    message = "Canceled";
                    identifier = String.Empty;
                    break;
                case AuthenticationStatus.Failed:
                    error = true;
                    message = "Failed";
                    identifier = String.Empty;
                    break;
            }
        }

        return Json(new { Error = error, Message = message, Identifier = identifier });
    }

If I understand your question, you are reaching this line of code: 如果我理解您的问题,那么您将到达以下代码行:

return _openId.CreateRequest(providerUrl).RedirectingResponse.AsActionResult();

but then the JavaScript doesn't know how to deal with the redirect result being returned from the Ajax call. 但是JavaScript却不知道如何处理Ajax调用返回的重定向结果。

In this case, I would say you are returning the wrong thing. 在这种情况下,我会说您要返回错误的内容。 Instead, you should probably do something like this: 相反,您可能应该执行以下操作:

return Json(new { RedirectUri = _openId.CreateRequest(providerUrl).RedirectingResponse.Uri });

but I don't know what kind of object RedirectingResponse is (eg does it have any kind of Uri property?). 但我不知道RedirectingResponse是哪种对象(例如,它具有任何Uri属性吗?)。 Just providerUrl won't work, right? 只是providerUrl不起作用,对吗?

Anyway, then in your JavaScript you could do 无论如何,那么您可以在JavaScript中进行

success: function (data)
{
    if (data.RedirectUri)
    {
        window.location.href = data.RedirectUri;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM