The problem is this:
During the execution of the request ($.post("/Account/OpenIdLogOn/"...) user was not authenticated.
Then, in your action method, you authenticate the user, but in the Request object that represents the request that the user created before the Auth cookie was created, the user did not authenticate. However, as you say in the following request, it works, because at this time the user has an Authentication cookie when he makes a request.
One solution here could be to create a viewmodel object to send from your method of action with controllers to your view. This viewModel may have a field called authenticated, and you can set it correctly from the action method. Then check this value instead of your view. I have not tested this in Visual Studio, but it should be something like this:
Create a view model:
public class LoginViewModel{ public bool Authenticated{ get; set; } }
Your method of action:
public ActionResult OpenIdLogOn(string token) { WebClient cli = new WebClient(); string json = cli.DownloadString(new Uri("http://ulogin.ru/token.php?token=" + Request.Params["token"] + "&host=" + Request.Url.Host)); var obj = JObject.Parse(json); var viewModel = new LoginViewModel{ Authenticated = Request.IsAuthenticated }; if (obj["error"] == null) { var userName = obj["nickname"].Value<string>(); var email = obj["email"].Value<string>(); FormsAuthentication.SetAuthCookie(userName, true); viewModel.Authenticated = true; } return PartialView("UserNavigation"); }
And your look
@model LoginViewModel @if (Model.Authenticated) { <a href="#" class="username"><span>@Context.User.Identity.Name</span><i class="icon iUser"></i></a> <ul class="headLine_link"> <li><a href="#">Profile</a></li> <li> @Html.ActionLink("Logg Off", "LogOff", "Account", null, new { @class = "exit" })</li> </ul> } else { <ul class="headLine_link"> <li><a id="regLink">Register</a></li> <li><a id="authLink">Log On</a></li> </ul> }
Creating a viewmodel instead of just sending the bool as a model is simply because I like to always send the data that I send to the view inside the view model. This makes it much easier to extend later, and makes reading easier inside the view (you can write @if (Model.Authenticated) instead of @if (Model) for example)
source share