为什么Request.IsAuthenticated是假的 [英] Why Request.IsAuthenticated is false

查看:99
本文介绍了为什么Request.IsAuthenticated是假的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本:

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

这是动作:

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");
        }

和,我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>
}

Request.IsAuthenticated 在该问题后,才刷新页面等于true。

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

推荐答案

问题如下:

在发出请求的时间($。员额(/帐号/ OpenIdLogOn /\"...)用户它未通过身份验证。

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

然后在你的操作方法,你验证用户,而是重新$ P $ Request对象上psents创建验证cookie之前所做的用户,该用户未通过身份验证的要求。然而,正如你在下一次请求时说,因为工作在当时的用户时,他的的请求。

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.

这里的一个解决方案是创建一个视图模型对象从你的控制器操作方法发送到您的视图。该视图模型可以有一个叫做认证领域,可以从操作方法正确设置它。然后检查这个值,而不是你的视图中。我没有在Visual Studio中检查这一点,但它应该是这样的:

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:

创建视图模型:

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

您的操作方法:

    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");
    }

和你的看法

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

创建视图模型,而不仅仅是发送一个布尔值作为模型只是因为我喜欢总是把我送到一个视图模型内部的视图中的数据。使得它更容易为以后扩展,并且可以更容易地在视图中读取(你可以写 @if(Model.Authenticated)而不是 @if(模型)为例)

这篇关于为什么Request.IsAuthenticated是假的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆