I am configuring bearer token authentication in Web API 2, and I do not understand how (or where) the token carrier is stored on the server side. Here is the relevant code:
Startup:
public partial class Startup { public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; } public static Func<UserManager<IdentityUser>> UserManagerFactory { get; set; } public static string PublicClientId { get; private set; } static Startup() { PublicClientId = "self"; UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>()); OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString("/Token"), Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory), AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"), AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), AllowInsecureHttp = true }; } public void ConfigureAuth(IAppBuilder app) {
WebApiConfig:
public class WebApiConfig { public static void ConfigureWebApi() { Register(GlobalConfiguration.Configuration); } public static void Register(HttpConfiguration http) { AuthUtil.ConfigureWebApiToUseOnlyBearerTokenAuthentication(http); http.Routes.MapHttpRoute("ActionApi", "api/{controller}/{action}", new {action = Actions.Default}); } }
AuthUtil:
public class AuthUtil { public static string Token(string email) { var identity = new ClaimsIdentity(Startup.OAuthOptions.AuthenticationType); identity.AddClaim(new Claim(ClaimTypes.Name, email)); var ticket = new AuthenticationTicket(identity, new AuthenticationProperties()); var currentUtc = new SystemClock().UtcNow; ticket.Properties.IssuedUtc = currentUtc; ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30)); var token = Startup.OAuthOptions.AccessTokenFormat.Protect(ticket); return token; } public static void ConfigureWebApiToUseOnlyBearerTokenAuthentication(HttpConfiguration http) { http.SuppressDefaultHostAuthentication(); http.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); } }
LoginController:
public class LoginController : ApiController { ... public HttpResponseMessage Post([FromBody] LoginJson loginJson) { HttpResponseMessage loginResponse; if () { var accessToken = AuthUtil.Token(loginJson.email); loginResponse = ; } else { loginResponse = ; } return loginResponse; } }
Using the code above, I can log in and save the client token on the client side in a cookie, and then make calls to the controllers marked with [Login], and it allows me.
My questions:
Where / how is the carrier token stored on the server side? This seems to be happening through one of the OWIN calls, but I can't tell where.
Can I transfer media tokens to the server-side server database so that they can remain in place after restarting the web API server?
If the answer to No. 2 is no, is there anyway a client to support its carrier token and reuse it even after the web API drops and returns? Although this may be rare in production, it can often happen with local testing.
authentication asp.net-web-api owin
dposada Jan 13 '14 at 22:01 2014-01-13 22:01
source share