Добрый вечер,
У меня есть веб-сайт, работающий на ASP.NET, вход в систему отлично работает для localhost, но не работает на рабочем месте: если пользователь входит в систему и обновляет страницу, то он принудительно выходит из системы. После некоторых вложений я обнаружил, что это происходит из-за куки-файла auth, он просто отсутствует в списке куки-файлов в _httpContext.Request (в методе GetAuthenticatedCustomer (), поскольку отсутствует куки-файл auth, _httpContext.Request.IsAuthenticated). ложно и метод возвращает ноль). Тем не менее, я вижу этот файл cookie в браузере, свойство домена установлено правильно, срок его действия не истек, и даже если установить дату окончания срока действия вручную, все не изменится. Использование FormsAuthentication.SetAuthCookie (name, true) также не помогает.
Вот часть моего кода службы FormsAuthenticationService:
public virtual void SignIn(Customer customer, bool createPersistentCookie)
{
var now = DateTime.UtcNow.ToLocalTime();
var ticket = new FormsAuthenticationTicket(
1 /*version*/,
_customerSettings.UsernamesEnabled ? customer.Username : customer.Email,
now,
now.Add(_expirationTimeSpan),
createPersistentCookie,
_customerSettings.UsernamesEnabled ? customer.Username : customer.Email,
FormsAuthentication.FormsCookiePath);
var encryptedTicket = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
cookie.HttpOnly = true;
if (ticket.IsPersistent)
{
cookie.Expires = ticket.Expiration;
}
cookie.Secure = FormsAuthentication.RequireSSL;
cookie.Path = FormsAuthentication.FormsCookiePath;
if (FormsAuthentication.CookieDomain != null)
{
cookie.Domain = FormsAuthentication.CookieDomain;
}
_httpContext.Response.Cookies.Add(cookie);
_cachedCustomer = customer;
}
public virtual void SignOut()
{
_cachedCustomer = null;
FormsAuthentication.SignOut();
}
public virtual Customer GetAuthenticatedCustomer()
{
if (_cachedCustomer != null)
return _cachedCustomer;
if (_httpContext == null ||
_httpContext.Request == null ||
!_httpContext.Request.IsAuthenticated ||
!(_httpContext.User.Identity is FormsIdentity))
{
return null;
}
var formsIdentity = (FormsIdentity)_httpContext.User.Identity;
var customer = GetAuthenticatedCustomerFromTicket(formsIdentity.Ticket);
if (customer != null && customer.Active && !customer.Deleted && customer.IsRegistered())
{
_cachedCustomer = customer;
}
return _cachedCustomer;
}
public virtual Customer GetAuthenticatedCustomerFromTicket(FormsAuthenticationTicket ticket)
{
if (ticket == null)
throw new ArgumentNullException("ticket");
var usernameOrEmail = ticket.UserData;
if (String.IsNullOrWhiteSpace(usernameOrEmail))
return null;
var customer = _customerSettings.UsernamesEnabled
? _customerService.GetCustomerByUsername(usernameOrEmail)
: _customerService.GetCustomerByEmail(usernameOrEmail);
return customer;
}
Единственное различие, которое я замечаю между localhost и production, заключается в том, что на производстве отсутствует файл cookie ASP.NET_SessionId. Однако я не знаю, насколько это важно.
Печенье при изготовлении
Печенье на localhost
Часть моего web.config:
<authentication mode="Forms">
<forms name="NOPCOMMERCE.AUTH" loginUrl="~/login" protection="All" timeout="43200" path="/" requireSSL="false" slidingExpiration="true" />
</authentication>
Целевой каркас - 4,5.