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

后端 未结 2 703
轻奢々
轻奢々 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条回答
  • 2020-12-14 21:01

    You can find how it could be done in the ServiceStack Use Cases repository. The following example is based on MVC4 but works perfectly for MVC3 either: CustomAuthenticationMvc.

    0 讨论(0)
  • 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<AuthResponse>("/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>();
            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.

    0 讨论(0)
提交回复
热议问题