簡體   English   中英

有沒有辦法在 asp.net MVC 的所有視圖中使用已識別的用戶“id”?

[英]Is there a way to use identified user 'id' in all views in asp.net MVC?

我想在所有視圖中登錄后使用“已識別”用戶的“id”,我該怎么做? 這是我在控制器中的“登錄”:

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

現在我想,當登錄中的用戶身份為“真”時,它的“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);

    }

這是我的觀點 model...

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

將ID另存為Session變量,每個視圖都可以獲取;

例子:

Session["ID"] = xyz;

xyz = 要傳遞給另一個視圖的變量

要調用它,只需使用例如:

@if (Session["ID"] != null)
{
your condition
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM