如何获取用户时区偏移量并将其发送到ASP.net应用程序中的服务器

时间:2016-11-09 15:50:36

标签: javascript asp.net asp.net-mvc asp.net-mvc-5 timezone

当用户成功登录时,我对capture/send用户的timezone/offset的看法感到困惑。在互联网上,我找到了“getTimezoneOffset”来完成工作。但我不知道如何发送一个在javascript中隐式计算的数据,而不是用户在登录表单中明确填写的数据。我是否需要一些隐藏的文件或触发javascript的内容然后捕获其值,我可以将其发送回服务器。请指导我。以下是我现在所拥有的

CSHTML文件:

@model LoginMV
@using SIM_Obj.ModelViews;

@{ 
    ViewBag.Title = "Tenant Login";
}

@using (Html.BeginForm("LoginSubmit", "Security", FormMethod.Post, new { id = "simLogin" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal-width-inherit">
        @Html.HiddenFor(model => model.ReturnUrl)

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
            @Html.LabelFor(model => model.Username, htmlAttributes: new { @class = "control-label ignoreSpacing col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Username, new { @class = "form-control" } )
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label ignoreSpacing col-md-2" })
            <div class="col-md-10">
                @Html.PasswordFor(model => model.Password, new { @class = "form-control" } )
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Login" class="btn btn-default" />
            </div>
        </div>
    </div>
}

视图模型:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LoginSubmit(LoginMV userData)
{
    if (userData != null && !string.IsNullOrEmpty(userData.Username) && !string.IsNullOrEmpty(userData.Password))
    {
        UserMV authenticatedUser = SecurityHelper.AuthenticateTenantUser(userData.Username, userData.Password);

        if (authenticatedUser == null)
        { ModelState.AddModelError("Username", "Incorrect"); }
        else
        {
            SimUtils.CurrentTenantId = authenticatedUser.Id;
            SimUtils.CurrentAccountId = authenticatedUser.AccountId;
        }
    }

    return View("Login", userData);
}

1 个答案:

答案 0 :(得分:2)

就个人而言,我会(并且已经)在我的剃刀表格上使用隐藏字段将额外数据发送到控制器。这可能是将用户的当前日期/时间传递到服务器的最简单方法。

我可能会这样做。

在您看来,添加

@Html.Hidden('UserLogInDate', null, new {id = "hidUserLoginDate"})

这将在视图上创建隐藏字段,为其指定ID。

然后你可以(应该)为登录视图提供单独的javascript文件。在那里,您可以在用户打开视图时使用客户日期/时间填充隐藏字段。

然后,您需要在模型中包含该字段,例如

public datetime UserLogInDate {get; set;}

通过它,您将能够处理日期/时间并在控制器中获取时区。

希望这有帮助