ASP.NET (OWIN) Identity: How to get UserID from a Web API controller?

You should be able to get user id on both MVC controller and web api controller by same extension method in identity 1.0 RTW package. Here is the extensions from identity package: namespace Microsoft.AspNet.Identity { public static class IdentityExtensions { public static string FindFirstValue(this ClaimsIdentity identity, string claimType); public static string GetUserId(this IIdentity identity); public … Read more

How do you login/authenticate a user with Asp.Net MVC5 RTM bits using AspNet.Identity?

So here’s what login will basically look like in RTM (code copied from the ASPNET Identity sample code): // // POST: /Account/Login [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { var user = await UserManager.FindAsync(model.UserName, model.Password); if (user != null) { await SignInAsync(user, model.RememberMe); return RedirectToLocal(returnUrl); } else { … Read more

Hw to pass arguments to my own Startup class?

If you want to pass parameter to StartUp class, you can use Action<IAppBuilder> in WebApp.Start like CilliĆ© Malan mentioned in the comment instead of launching with Type parameter(WebApp.Start<T>) Here is a concrete example for self-hosting: object someThingYouWantToAccess; var server = WebApp.Start(“http://localhost:8080/”, (appBuilder) => { // You can access someThingYouWantToAccess here // Configure Web API for … Read more

Why is [Owin] throwing a null exception on new project?

Similar to Sandeep’s answer, I also updated the cookie authentication provider. Except, instead of swallowing the error I threw the exception so you could see what the underlying problem was: app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString(“/Account/Login”), Provider = new CookieAuthenticationProvider { // Enables the application to validate the security stamp when … Read more

IHttpActionResult and helper methods in ASP.NET Core

IHttpActionResult is now effectively IActionResult, and to return an Ok with a return object, you’d use return new ObjectResult(…); So effectively something like this: public IActionResult Get(int id) { if (id == 1) return HttpNotFound(“not found!”); return new ObjectResult(“value: ” + id); } Here’s a good article with more detail: http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6

UseOAuthBearerTokens vs UseOAuthBearerAuthentication

The UseOAuthBearerTokens extension method creates both the token server and the middleware to validate tokens for requests in the same application. Pseudocode from source using reflector: UseOAuthAuthorizationServer(); // authorization server middleware UseOAuthBearerAuthentication(ApplicationOAuthBearerProvider); // application bearer token middleware UseOAuthBearerAuthentication(ExternalOAuthBearerProvider); // external bearer token middleware

Context.User.Identity.Name is null with SignalR 2.X.X. How to fix it?

I found the final solution, this is the code of my OWIN startup class: public void Configuration(IAppBuilder app) { app.MapSignalR(); // Enable the application to use a cookie to store information for the signed i user app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString(“/Home/Index”) }); // Use a cookie to temporarily store information … Read more

What is the purpose of the extension method CreatePerOwinContext in OWIN implementation by Microsoft

CreatePerOwinContext registers a static callback which your application will use to get back a new instance of a specified type. This callback will be called once per request and will store the object/objects in OwinContext so that you will be able to use them throughout the application. Let’s say you have defined your own implementation … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)