Я хочу использовать «идентификатор» «идентифицированного» пользователя после входа в систему во всех моих представлениях, как я могу это сделать? это мой 'логин' в контроллере:
[AllowAnonymous]
[HttpPost]
public ActionResult Login(string username, string password, bool rememberme)
{
UserRepository RepU = new UserRepository();
if (this.IsCaptchaValid("Captcha is not valid"))
{
if (RepU.Exist(username, password))
{
string rol = RepU.Where(p => p.Username == username).Single().Roles;
int ViD = RepU.Where(p => p.Username == username).Single().Id;
ProductCategoryViewModel vMprocat = new ProductCategoryViewModel();
RedirectToAction("VisitorAddProduct","AdminPanel",new{id = ViD});
int timeout = rememberme ? 2880 : 60;
var ticket = new FormsAuthenticationTicket(username, rememberme, timeout);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
FormsAuthentication.SetAuthCookie(username, rememberme);
cookie.Expires = DateTime.Now.AddMinutes(timeout);
Response.Cookies.Add(cookie);
if (rol == "Visitor")
{
return Redirect("/AdminPanel/Dashboard");
}
else if (rol == "Customer")
{
return Redirect("/AdminPanel/CustomerHistoryInvoice");
}
else if (rol == "GreatAdmin")
{
return Redirect("/AdminPanel/AdminManageStatus");
}
else
{
return Redirect("/Home/Index");
}
}
else
{
ViewBag.LoginErrorClass = "alert alert-danger";
ViewBag.LoginErrorStyle = "display:block";
ViewBag.LoginErrorIcon = "fa fa-frown-o mr-2";
ViewBag.LoginErrorMessage = "IncorrectUnameorPass";
return View();
}
}
else
{
ViewBag.LoginErrorClass = "alert alert-warning";
ViewBag.LoginErrorStyle = "display:block";
ViewBag.LoginErrorIcon = "fa fa-exclamation mr-2";
ViewBag.LoginErrorMessage = "Incorrectcaptcha!!!";
return View("Login");
}
}
и это также ...
[AllowAnonymous]
[HttpGet]
public ActionResult Login()
{
if (User.Identity.IsAuthenticated)
{
UserRepository repU = new UserRepository();
string rol = repU.Where(p => p.Username == User.Identity.Name).Single().Roles;
if (rol == "Visitor")
{
return Redirect("/AdminPanel/Dashboard");
}
else if (rol == "Customer")
{
return Redirect("/AdminPanel/CustomerHistoryInvoice");
}
else if (rol == "GreatAdmin")
{
return Redirect("/AdminPanel/AdminManageStatus");
}
else
{
return Redirect("/Home/Index");
}
}
else
{
ViewBag.LoginErrorClass = "";
ViewBag.LoginErrorStyle = "";
ViewBag.LoginErrorIcon = "";
ViewBag.LoginErrorMessage = "";
return View();
}
}
Теперь я хочу, когда идентификатор пользователя в логине был 'True', это 'ID 'перейти к представлению, чтобы показать или использовать, это результат действия этого представления .....
[Authorize(Roles = "Visitor")]
public ActionResult VisitorAddProduct(string CategoryName , string CompanyName, int? page ,int id)
{
ProductCategoryViewModel VMprocat = new ProductCategoryViewModel();
CategoryRepository repCat = new CategoryRepository();
CompanyRepository repCom = new CompanyRepository();
ProductRepository repPro = new ProductRepository();
ScopeRepository repSco = new ScopeRepository();
ProductStatusRepository repPS = new ProductStatusRepository();
VMprocat.Category = repCat.Select();
VMprocat.Company = repCom.Select();
VMprocat.Scope = repSco.Select();
VMprocat.ProductStatu = repPS.Select();
var firstCat = VMprocat.Category.First();
if (firstCat != null)
{
int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
if (String.IsNullOrEmpty(CategoryName))
{
CategoryName = firstCat.CategoryName;
}
else
{
CategoryName = CategoryName.Trim();
}
VMprocat.Product = repPro.Where(p => p.Category.CategoryName == CategoryName)
.ToList().OrderBy(p => p.ProductName).ToPagedList(currentPageIndex, 10);
}
else
{
VMprocat.Category = null;
}
return View("VisitorAddProduct", VMprocat);
}
и это моя модель представления ...
public class ProductCategoryViewModel
{
public IPagedList<MSCMTestProject3.Models.DomainModels.Product> Product { get; set; }
public IEnumerable<MSCMTestProject3.Models.DomainModels.Category> Category { get; set; }
public IEnumerable<MSCMTestProject3.Models.DomainModels.Company> Company { get; set; }
public IEnumerable<MSCMTestProject3.Models.DomainModels.Scope> Scope { get; set; }
public IEnumerable<MSCMTestProject3.Models.DomainModels.ProductStatu> ProductStatu { get; set; }
public int? Id { get; set; }
public string CategoryName { get; set; }
public string CompanyName { get; set; }
public string ScopeName { get; set; }
}