简体   繁体   中英

Get ServiceStack session in MVC.Net attribute

I'm using MVC.Net and servicestack with AuthFeature

Plugins.Add(new AuthFeature(() => new AuthUserSession(),
                new IAuthProvider[]
                {
                    new CredentialsAuthProvider()
                })
                {   IncludeRegistrationService = true });

I want to do a MVC.NET filter or attribute to make some validation and redirection before action methods execute, but i need the user session to do it.

public class UserConfirmationAttribute : FilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        //Get service stack user session
    }
}

In a service or controller is easy to get the session, only have to use SessionAs<AuthUserSession>() . How can I get service stack user session inside a MVC.Net Attribute?

If your FilterAttribute is for an action on a MVC Controller inheriting from ServiceStackController you can resolve it from the Controller with:

var ssController = filterContext.Controller as ServiceStackController;
var session = ssController.ServiceStackProvider.SessionAs<AuthUserSession>();

Otherwise you can get the session from a new ServiceStackProvider instance with:

var ssProvider = new ServiceStackProvider(filterContext.HttpContext.ToRequest());
var session = ssProvider.SessionAs<AuthUserSession>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM