Как я ограничиваю доступ к определенным страницам в ASP.NET MVC?

Pure Python:

from datetime import datetime
my_datetime = datetime.strptime('2014-05-18', '%Y-%m-%d') 
repr(my_datetime)

>>> 'datetime.datetime(2014,5,18,0,0)'

Проверьте документацию формата datetime.strptime () для получения дополнительных строк формата.

5
задан Pure.Krome 3 June 2009 в 13:48
поделиться

3 ответа

3
ответ дан 14 December 2019 в 04:46
поделиться

Для этого я использую настраиваемый атрибут, производный от AuthorizeAttribute . Переопределите метод OnAuthorize и реализуйте свою собственную логику.

public class OnlyUserAuthorizedAttribute : AuthorizeAttribute
{
    public override void OnAuthorize( AuthorizationContext filterContext )
    {
        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            filterContext.Result = new HttpUnauthorizeResult();
        }
        ...
    }
}
3
ответ дан 14 December 2019 в 04:46
поделиться

I implemented the following ActionFilterAttribute and it works to handle both authentication and roles. I am storing roles in my own DB tables like this:

  • User
  • UserRole (contains UserID and RoleID foreign keys)
  • Role
public class CheckRoleAttribute : ActionFilterAttribute
{
    public string[] AllowedRoles { get; set; }


    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        string userName = filterContext.HttpContext.User.Identity.Name;

        if (filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            if (AllowedRoles.Count() > 0)
            {
                IUserRepository userRepository = new UserRepository();
                User user = userRepository.GetUser(userName);
                bool userAuthorized = false;
                foreach (Role userRole in user.Roles)
                {
                    userAuthorized = false;
                    foreach (string allowedRole in AllowedRoles)
                    {
                        if (userRole.Name == allowedRole)
                        {
                            userAuthorized = true;
                            break;
                        }
                    }
                }
                if (userAuthorized == false)
                {
                    filterContext.HttpContext.Response.Redirect("/Account/AccessViolation", true);
                }
            }
            else
            {
                filterContext.HttpContext.Response.Redirect("/Account/AccessViolation", true);
            }
        }
        else
        {
            filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl + String.Format("?ReturnUrl={0}", filterContext.HttpContext.Request.Url.AbsolutePath), true);
        }


    }

I call this like this...

    [CheckRole(AllowedRoles = new string[] { "admin" })]
    public ActionResult Delete(int id)
    {
        //delete logic here
    }
2
ответ дан 14 December 2019 в 04:46
поделиться
Другие вопросы по тегам:

Похожие вопросы: