Bir asp.net-mvc5 proyect içinde HttpContext.Current.User.Identity oturum açma hakkında bilgi kaydetmeye çalışıyorum. Ama her şey boş.MVC Giriş. User.Identity her zaman boş
Her şeyden önce, denetleyicili bir Görünüm var. İlk kullanıcı veritabanı üzerinde olduğunu kontrol edin
public ActionResult Button1_Click(string user, string pass)
{
bool result = _model.ValidateLogin(user, pass, 3, false);
DirectResult r = new DirectResult();
// Do some Authentication...
if (!result)
{
r.Success = false;
r.ErrorMessage = "Invalid username or password.";
}
return r;
}
yöntem ValidateLogin ve kimliğine sahip bir çerez oluşturmak: Eğer giriş butonuna tıkladığınızda , kontrolöre çağrı
DateTime now = DateTime.Now;
System.Web.Security.FormsAuthentication.Initialize();
System.Web.Security.FormsAuthenticationTicket ticket = new System.Web.Security.FormsAuthenticationTicket(1, userId.ToString(), now, now.Add(System.Web.Security.FormsAuthentication.Timeout), checkRemember, string.Empty, System.Web.Security.FormsAuthentication.FormsCookiePath);
string hash = System.Web.Security.FormsAuthentication.Encrypt(ticket);
System.Web.HttpCookie cookie = new System.Web.HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, hash);
if (ticket.IsPersistent)
cookie.Expires = now.AddYears(1);
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
Ama ne zaman ben her zaman boştur ve "IsAuthenticated" false User.Identify kontrol etmek deneyin:
protected override System.Security.Principal.IIdentity GetClientIdentity()
{
IIdentity identity = System.Web.HttpContext.Current.User.Identity;
if (identity.IsAuthenticated)
return identity;
else
throw new AuthorizationDeniedException("Not logged in",false);
}
Neden? Herhangi bir fikir? Eklenti web.config için
Düzenleme: web config olarak
i var:
<authentication mode="Forms">
<forms loginUrl="~/Login/Index" protection="All" timeout="120" name=".ASPXAUTH" path="/" requireSSL="false" slidingExpiration="true" defaultUrl="default.html" cookieless="UseDeviceProfile" enableCrossAppRedirects="false"/>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
<location path="Login">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
Sadece 'Application_PostAuthenticateRequest' sürümleri ile çalışılıyorsa işaret edilir. –