获取asp.NET MVC中的当前用户,以便我可以检查用户控件中的操作

时间:2021-11-13 03:38:30

I am using ASP.NET MVC for a project. I use a lot of User Control and I need to check the current user and the check if it has the roles etc, now I create the user in every UserControl I see the Permissions. I want to change that so I create it only once.

我正在使用ASP.NET MVC进行项目。我使用了很多用户控件,我需要检查当前用户并检查它是否有角色等,现在我在每个UserControl中创建用户我看到了权限。我想改变它,所以我只创建一次。

the Question is Whta is the best aproch? viewData["User"] = user and the get the user form here or what? what do you recomend so I can get rid of this lines

问题是什么是最好的aproch? viewData [“User”] =用户和获取用户表单在这里或什么?你有什么建议,所以我可以摆脱这条线

 LCP.eTorneos.Dal.EntityFramework.JugadorRepository jugadorRepository =
                   new LCP.eTorneos.Dal.EntityFramework.JugadorRepository();
 var jugador = jugadorRepository.GetJugador(User.Identity.Name)
 <% if (Page.User.Identity.IsAuthenticated && jugador.IsAdmin) { %>
      ...
 <%}%>

3 个解决方案

#1


I believe that something like this:

我相信这样的事情:

<%= Utils.GetJugador(ViewData).IsAdmin %>

is much better than this:

比这更好:

<%= Html.GetJugador().IsAdmin %>

because HtmlHelper extensions are only for generating HTML markup

因为HtmlHelper扩展仅用于生成HTML标记

UPDATE:

using System.Web.Mvc;
using LCP.eTorneos.Dal.EntityFramework;

public static class Utils {
    public static Jugador GetJugador(ViewDataDictionary ViewData) {
        return ViewData["JugadorActual"] as Jugador;
        /* OR maybe ?
         * return (Jugador)(ViewData["JugadorActual"] ?? new Jugador());
         */
    }
}

Hope this helps

希望这可以帮助

#2


There are 2 options. First, using ViewData["User"] - the simplest but not the best (not strongly typed). Second (if you are using View Models), using Base View Model for all your View Models:

有2个选项。首先,使用ViewData [“User”] - 最简单但不是最好的(不是强类型)。第二个(如果您使用的是View Models),使用所有View模型的Base View Model:

public class BaseViewModel {
    public Jugador Jugador;

    // Or simply add flag

    public IsAdmin;
}

public class ConcreteViewModel : BaseViewModel {
    public YourModel Model;
}

In Controller:

var model = new ConcreteViewModel {
    Model = yourModel,
    IsAdmin = true /* false */
};

return View(model);

In Views:

<%@ Page MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ConcreteViewModel>" %>

<!-- Or in SiteMaster: -->

<%@ Master Inherits="System.Web.Mvc.ViewMasterPage<BaseViewModel>" %>

<% if(Model.IsAdmin) { %>

...

<% } %>

UPDATED:

It is better to avoid duplicating your code and setup the base part of ViewModel using custom filter:

最好避免使用自定义过滤器复制代码并设置ViewModel的基本部分:

public class IsAdminAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        // ...

        (filterContext.Controller.ViewData.Model as BaseViewModel).IsAdmin = true; /* flase */
    }
}

#3


First of all Thanks @eu-ge-ne.

首先谢谢@ eu-ge-ne。

This I what I did, I am open to new suggestions but this seems to work: I create a ActionFilterAttribute like this:

这就是我所做的,我对新的建议持开放态度,但这似乎有效:我创建了一个ActionFilterAttribute,如下所示:

 public class JugadorAttribute : ActionFilterAttribute {
    public override void OnActionExecuted(ActionExecutedContext filterContext) {
        JugadorRepository jugadorRepository = new JugadorRepository();
        Jugador jug = jugadorRepository.GetJugador(filterContext.HttpContext.User.Identity.Name);
        filterContext.Controller.ViewData["JugadorActual"] = jug; 
    }
}

This put in ViewData the current Player of the Page. Then in my controller I do this:

这将ViewData放在页面的当前播放器中。然后在我的控制器中我这样做:

 [JugadorAttribute()]
public class HomeController : Controller {

The Problem now Is that ViewData is not strong typed so I create this helper in the Html class:

现在的问题是ViewData不是强类型的,所以我在Html类中创建了这个帮助:

 public static class JugadorHelper {
    public static Jugador GetJugador(this HtmlHelper html) {
        return ((LCP.eTorneos.Dal.EntityFramework.Jugador)html.ViewData["JugadorActual"]);
    }
}

And Whoala, now I can do this in my views:

而Whoala,现在我可以在我的观点中做到这一点:

Html.GetJugador().IsAdmin

#1


I believe that something like this:

我相信这样的事情:

<%= Utils.GetJugador(ViewData).IsAdmin %>

is much better than this:

比这更好:

<%= Html.GetJugador().IsAdmin %>

because HtmlHelper extensions are only for generating HTML markup

因为HtmlHelper扩展仅用于生成HTML标记

UPDATE:

using System.Web.Mvc;
using LCP.eTorneos.Dal.EntityFramework;

public static class Utils {
    public static Jugador GetJugador(ViewDataDictionary ViewData) {
        return ViewData["JugadorActual"] as Jugador;
        /* OR maybe ?
         * return (Jugador)(ViewData["JugadorActual"] ?? new Jugador());
         */
    }
}

Hope this helps

希望这可以帮助

#2


There are 2 options. First, using ViewData["User"] - the simplest but not the best (not strongly typed). Second (if you are using View Models), using Base View Model for all your View Models:

有2个选项。首先,使用ViewData [“User”] - 最简单但不是最好的(不是强类型)。第二个(如果您使用的是View Models),使用所有View模型的Base View Model:

public class BaseViewModel {
    public Jugador Jugador;

    // Or simply add flag

    public IsAdmin;
}

public class ConcreteViewModel : BaseViewModel {
    public YourModel Model;
}

In Controller:

var model = new ConcreteViewModel {
    Model = yourModel,
    IsAdmin = true /* false */
};

return View(model);

In Views:

<%@ Page MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ConcreteViewModel>" %>

<!-- Or in SiteMaster: -->

<%@ Master Inherits="System.Web.Mvc.ViewMasterPage<BaseViewModel>" %>

<% if(Model.IsAdmin) { %>

...

<% } %>

UPDATED:

It is better to avoid duplicating your code and setup the base part of ViewModel using custom filter:

最好避免使用自定义过滤器复制代码并设置ViewModel的基本部分:

public class IsAdminAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        // ...

        (filterContext.Controller.ViewData.Model as BaseViewModel).IsAdmin = true; /* flase */
    }
}

#3


First of all Thanks @eu-ge-ne.

首先谢谢@ eu-ge-ne。

This I what I did, I am open to new suggestions but this seems to work: I create a ActionFilterAttribute like this:

这就是我所做的,我对新的建议持开放态度,但这似乎有效:我创建了一个ActionFilterAttribute,如下所示:

 public class JugadorAttribute : ActionFilterAttribute {
    public override void OnActionExecuted(ActionExecutedContext filterContext) {
        JugadorRepository jugadorRepository = new JugadorRepository();
        Jugador jug = jugadorRepository.GetJugador(filterContext.HttpContext.User.Identity.Name);
        filterContext.Controller.ViewData["JugadorActual"] = jug; 
    }
}

This put in ViewData the current Player of the Page. Then in my controller I do this:

这将ViewData放在页面的当前播放器中。然后在我的控制器中我这样做:

 [JugadorAttribute()]
public class HomeController : Controller {

The Problem now Is that ViewData is not strong typed so I create this helper in the Html class:

现在的问题是ViewData不是强类型的,所以我在Html类中创建了这个帮助:

 public static class JugadorHelper {
    public static Jugador GetJugador(this HtmlHelper html) {
        return ((LCP.eTorneos.Dal.EntityFramework.Jugador)html.ViewData["JugadorActual"]);
    }
}

And Whoala, now I can do this in my views:

而Whoala,现在我可以在我的观点中做到这一点:

Html.GetJugador().IsAdmin