Unbreakable Unbreakable - 1 month ago 12
ASP.NET (C#) Question

How to get user timezone offset and send it to the server in ASP.net application

I am confused on how

capture/send
user's
timezone/offset
when user does a successful sign in. On internet I found "getTimezoneOffset" to get the work done. But I don't know how to send a data which is calculated implicitly in javascript and not filled explicitly by user in the login in form. Do I need some hidden filed or something which triggers the javascript and then captures its value which I can send back to the server. Please guide me. Below is what I have till now

CSHTML File:

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


ViewModel:

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

Answer

Personally, I would (and have) used hidden fields on my razor form to send extra data up to the controller. That may be the easiest way to the pass the user's current date/time up to your server.

I might do something like this.

In your view, add

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

That will create the hidden field on your view giving it the ID as specified.

Then you may (should) have a separate javascript file for the login view. In there you can populate the hidden field with the client date/time as the user opens the view.

You then need to include that field in you model, such as

public datetime UserLogInDate {get; set;}

With that you will be able to process the date/time and get the timezone in your controller.

Hope this helps