Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7570667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:29:03+00:00 2026-05-30T15:29:03+00:00

I have the following script: function OpenIdLogon(e) { $.post(/Account/OpenIdLogOn/, { token: e }, function

  • 0

I have the following script:

function OpenIdLogon(e) {
        $.post("/Account/OpenIdLogOn/", { token: e }, function (data) {
            $("#userNavigation").html(data);
            $(".auth_box").hide();
            $(".kb_fading").hide();
        });
    }

This is the 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);
            if (obj["error"] == null)
            {
                var userName = obj["nickname"].Value<string>();
                var email = obj["email"].Value<string>();
                FormsAuthentication.SetAuthCookie(userName, true);                    
            }

            return PartialView("UserNavigation");
        }

And, my UserNavigation:

@if (Request.IsAuthenticated)
{
    <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>
}

The problem in that Request.IsAuthenticated equals true only after refresh page.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:29:04+00:00Added an answer on May 30, 2026 at 3:29 pm

    The problem is the following:

    At the time of making the request ($.post("/Account/OpenIdLogOn/"...) the user it not authenticated.

    Then in your action method you authenticate the user, but on the Request object that represents the request the user made before you create the Auth cookie, the user was not authenticated. However, as you say on the next request it works since at that time the user has the Authentication cookie when he makes the request.

    One solution here can be to create a viewmodel object to send from your controllers action method to your view. This viewModel can have a field called authenticated, and you can set it properly from the action method. Then check this value instead inside your view. I haven’t checked this in Visual Studio, but it should be something like this:

    Create the viewmodel:

    public class LoginViewModel{
      public bool Authenticated{ get; set; }
    }
    

    Your action method:

        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 view

    @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 a bool as model is just because I like to always put the data I send to a view inside a viewmodel. Makes it much easier to extend later on, and makes it easier to read inside the view (you can write @if (Model.Authenticated) instead of @if (Model) for example)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script: $("tr.member_profile_comment_row_frame").mouseover(function () { $(this.element."img.member_profile_comment_show_delete_button").show(); }); $("tr.member_profile_comment_row_frame").mouseout(function () { $(this.element."img.member_profile_comment_show_delete_button").hide();
I have the following javascript function: <script type=text/javascript> function quickCardRegister_OnCompleteSave() { publishContent('This is a
I have the following script: $('#divs').children().each( function(){ //Get Id's var id = $(this).attr('id').replace('id_',''); alert(id);
I have the following script: $(function() { $(.message).hide(); function simulate_ajax_call() { $.ajax({ url: /echo/json/,
I have the following script: $(.Text).contents().each(function () { $(this).replaceWith($(this).text() .replace(/\[([^\]]*)\]/g, '<span class=IT_Symbol style=display:inline;border: 1px
I have the following script: $(document).ready(function() { $('#slideSelect').change(function(){ $('#slideViewer img').attr('src', $(this).val() + '.png'); });
I have following script: $('.news ul li').hide(); $('.news ul li:first-child').show(); function Roller() { var
I have the following script -- $(document).ready(function () { $(div.settings_frame_tabs_frame).(function () { $(div.settings_frame_tabs_heading ,this).click(function
I have the following script: $('#element_1').live('click', function() { function_1(this) }); function function_1(that) { that
I have the following script inside a HTML page: <script> function Test(){ alert(i got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.