How to use ServiceStack authentication correctly in ASP.Net MVC controller

后端 未结 2 790
轻奢々
轻奢々 2020-12-14 20:26

I\'m having problem with getting ServiceStack [Authentication] attribute to work in ASP.Net MVC4 controller, pages / action methods with the attribute keep redirecting Users

2条回答
  •  -上瘾入骨i
    2020-12-14 21:05

    After much fiddling around, apparently the way to hook ServiceStack authentication is to call the AuthService via:

    try {
        authResponse = AuthService.Authenticate(new Auth{ UserName = model.UserName, Continue = returnUrl, Password = model.Password });
    } catch (Exception ex) {
        // Cut for brevity...
    }
    

    and NOT authResponse = this.CreateRestClient().Post("/auth/credentials", model);!

    Where AuthService is defined in the base controller as:

    public AuthService AuthService
    {
        get
        {
            var authService = ServiceStack.WebHost.Endpoints.AppHostBase.Instance.Container.Resolve();
            authService.RequestContext = new HttpRequestContext(
                System.Web.HttpContext.Current.Request.ToRequest(),
                System.Web.HttpContext.Current.Response.ToResponse(),
                null);
    
            return authService;
        }
    }
    

    Everything else (incl. session) works correctly now.

提交回复
热议问题