I’m using DotNetOpenAuth with a custom login provider to sign users into my site. 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. I expected this, as I need to get the login results back to the 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).
Below is the code:
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:
but then the JavaScript doesn’t know how to deal with the redirect result being returned from the Ajax call.
In this case, I would say you are returning the wrong thing. Instead, you should probably do something like this:
but I don’t know what kind of object
RedirectingResponseis (e.g. does it have any kind ofUriproperty?). JustproviderUrlwon’t work, right?Anyway, then in your JavaScript you could do