Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Implement an action filter of your own, which will centrally detect the current logged in user and apply it to a parameter of your function. Something like this:</p> <pre><code>[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)] public class UserIDFilter : ActionFilterAttribute { public bool Require { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { if (Require) { if (Membership.GetUser() == null) { string formsAuthenticationToken = HttpContext.Current.Request.Form["cookie"]; if (!string.IsNullOrEmpty(formsAuthenticationToken)) { FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(formsAuthenticationToken); if (ticket != null) { FormsIdentity identity = new FormsIdentity(ticket); string[] roles = Roles.GetRolesForUser(identity.Name); GenericPrincipal principal = new GenericPrincipal(identity, roles); HttpContext.Current.User = principal; } } if (Membership.GetUser() == null) throw new UserNotAuthenticatedException(); } } const string key = "userId"; if (filterContext.ActionParameters.ContainsKey(key)) { if (Membership.GetUser() != null) filterContext.ActionParameters[key] = (Guid)Membership.GetUser().ProviderUserKey; else filterContext.ActionParameters[key] = (Guid?)null; } base.OnActionExecuting(filterContext); } } </code></pre> <p>And then decorate each action with:</p> <pre><code>[UserIDFilter] (and the action will have 1st parameter: Guid? userId) </code></pre> <p>or</p> <pre><code>[UserIDFilter(Require=true) (and the action will have 1st parameter: Guid userId) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload