Note that there are some explanatory texts on larger screens.

plurals
  1. POServiceStack - Custom CredentialsAuthProvider within .Net MVC app
    primarykey
    data
    text
    <p>I am attempting to authenticate against MVC and ServiceStack following the example here - <a href="https://github.com/ServiceStack/ServiceStack.UseCases/tree/master/CustomAuthenticationMvc" rel="nofollow">https://github.com/ServiceStack/ServiceStack.UseCases/tree/master/CustomAuthenticationMvc</a>. My issue is that I am unable to authenticate successfully against ServiceStack on my initial request to Account/LogOn. </p> <p>ServiceStack related code in LogOn method of AccountController: </p> <pre><code>var apiAuthService = AppHostBase.Resolve&lt;AuthService&gt;(); apiAuthService.RequestContext = System.Web.HttpContext.Current.ToRequestContext(); var apiResponse = apiAuthService.Authenticate(new Auth { UserName = model.UserName, Password = model.Password, RememberMe = false }); </code></pre> <p>I have a custom Authentication Provider that subclasses CredentialsAuthProvider. I Configure as follows in the AppHost class:</p> <pre><code>var appSettings = new AppSettings(); Plugins.Add(new AuthFeature(() =&gt; new CustomUserSession(), new IAuthProvider[] { new ActiveDirectoryAuthProvider(), })); </code></pre> <hr> <pre><code>public override bool TryAuthenticate(ServiceStack.ServiceInterface.IServiceBase authService, string userName, string password) { //class to authenticate against ActiveDirectory var adAuthentication = new ActiveDirectoryAuthenticationService(); if (!adAuthentication.Authenticate(userName, password)) return false; var session = (CustomUserSession)authService.GetSession(false); session.IsAuthenticated = true; session.UserAuthId = session.UserAuthName; authService.SaveSession(session, SessionExpiry); return true; } </code></pre> <p>I think my issue is that <strong>session.Id</strong> is null at this point and saving the session persists 'urn:iauthsession:' to the 'SessionCache'. However, I'm not sure how to correctly populate <strong>session.Id</strong>. Also, this may or may not be an issue, but the initial LogOn request is to Account/Logon which is handled by MVC. So, there is no request to ServiceStack prior to the AuthService.Authenticate() call in the AccountController. </p> <p>A possible solution I came up with has been added below in my subclass of CredentialsAuthProvider. </p> <pre><code>public override bool TryAuthenticate(ServiceStack.ServiceInterface.IServiceBase authService, string userName, string password) { //class to authenticate against ActiveDirectory var adAuthentication = new ActiveDirectoryAuthenticationService(); if (!adAuthentication.Authenticate(userName, password)) return false; var session = (CustomUserSession)authService.GetSession(false); //A possible solution??? if(session.Id == null) { var req = authService.RequestContext.Get&lt;IHttpRequest&gt;(); var sessId = HttpContext.Current.Response.ToResponse().CreateSessionIds(req); session.Id = sessId; req.SetItem(SessionFeature.SessionId, sessId); } //end possible solution session.IsAuthenticated = true; session.UserAuthId = session.UserAuthName; authService.SaveSession(session, SessionExpiry); return true; } </code></pre> <p>Is there a configuration or call I'm missing to 'wire up' ServiceStack Authentication within MVC?</p> <p>Thanks. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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