1

Я пытаюсь реализовать аутентификацию с помощью ActionFilterAttribute в MVC, и я ищу какой-то образец проекта mvc, который сделал этот тип аутентификации?ASP.NET MVC: Где я могу получить образцы проектов MVC?

+0

http://www.asp.net/aspnet/samples/aspnet-mvc –

+0

вот демонстрационный пример: http: // prodin ner.aspnetawesome.com/account/SignIn, он использует Microsoft.Owin и связанные пакеты для Auth, более старые версии, используемые FormsAuthentication, вы можете загрузить текущие и более старые версии на http://prodinner.codeplex.com/ – Omu

ответ

0

Там не очень много для самого действия фильтра, конкретная проверка подлинности будет зависеть от ваших требований, которые вы не подробный:

/// <summary> 
/// Custom authorization attribute for use on controllers and actions. 
/// Throws an exception if the user is not authorized. 
/// </summary> 
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] 
public sealed class ActionPermissionAttribute : AuthorizeAttribute 
{ 
    public override void OnAuthorization(AuthorizationContext filterContext) 
    { 
     // Override OnAuthorization, not AuthorizeCore as AuthorizeCore will force user login prompt rather than inform the user of the issue. 

     var controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; 
     var action = filterContext.ActionDescriptor.ActionName; 
     var user = HttpContext.Current.Request.LogonUserIdentity; 

     // Check user can use the app or the specific controller/action 
     var authorised = ... 

     if (!authorised) 
      throw new UnauthorizedAccessException("You are not authorised to perform this action."); 

     base.OnAuthorization(filterContext); 
    } 
} 
Смежные вопросы