Arama butonu
Bu konudaki kullanıcılar: 1 misafir
0
Cevap
633
Tıklama
0
Öne Çıkarma
Asp.net mvc authorization sorunu?
K
5 yıl
Çavuş
Konu Sahibi

Arkadaşlar login işleminde role göre yönlendirme yapmaya çalışıyorum.Login formu olmadan kullanıcı adı ve parolayı url üzerinden gönderdiğimde akif kullanıcının rolü IsInrole metodu ile true dönüyor fakat login formu yapıp bilgileri post ettiğimde ilk girişte IsInRole metodu ilk girişte false dönüyor ikinci girişte true dönüyor.Nasıl çözebilirim acaba bu sorunu?
String dönen ve sorunsuz çalışan kod
public string Login(string userName, string password)
{
var user = _userService.GetByUserNameAndPassword(userName, password);
if (user != null)
{
AuthenticationHelper.CreateAuthCookie(
new Guid(), user.UserName,
user.Email,
DateTime.Now.AddDays(15),
_userService.GetUserRoles(user).Select(u => u.RoleName).ToArray(),
false,
user.FirstName,
user.LastName);
return "User is authenticated!";
}
return "User is NOT authenticated!";
}

İkinci login işleminde çalışan kod.ilkinde çalışmıyor.

 public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(string userName, string password)
{

var hash = HashwordHelper.GenerateSHA512String(password);
var user = _userService.GetByUserNameAndPassword(userName, hash);
if (user != null)
{
AuthenticationHelper.CreateAuthCookie(
new Guid(), user.UserName, user.Email,
DateTime.Now.AddMinutes(60), _userService.GetUserRoles(user).Select(u => u.RoleName).ToArray(),
true, user.FirstName, user.LastName);
bool detect = HttpContext.User.IsInRole("Student");
bool detect2 = System.Threading.Thread.CurrentPrincipal.IsInRole("Student");
if (HttpContext.User.IsInRole("Admin") || HttpContext.User.IsInRole("Editor"))
{
return RedirectToAction("Index", "Admin");
}
else if (HttpContext.User.IsInRole("Student"))
{
return RedirectToAction("Index", "Product");
}
}
return View();
}



DH Mobil uygulaması ile devam edin. Mobil tarayıcınız ile mümkün olanların yanı sıra, birçok yeni ve faydalı özelliğe erişin. Gizle ve güncelleme çıkana kadar tekrar gösterme.