The following Code is not creating .ASPNET Cookie, I am using this code in WebAPI custom login method
//TODO Validate Credential
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, "ABCDE"));
claims.Add(new Claim(ClaimTypes.Email, "[email protected]"));
var id = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
var authmgr = Request.GetOwinContext().Authentication;
authmgr.SignIn(id);
When the OWIN runtime fires up, it looks via Reflection for a class named Startup class with a Configuration (IAppBuilder app) method and when it finds it executes that method. ConfigureAuth () then configures Cookie Authentication for local logins and the external providers for Google, Twitter and Github.
To use cookie authentication, you need to state that as your intention plus set the default authentication type: To add the authentication middleware, Okta have made it really easy by adding an extension method specifically to the IAppBuilder instance, that you call with predefined options:
If you’ve used FormsAuthentication in ASP.NET you know that there’s a global object that handles management of the user tracking cookie that associates a user with an account. OWIN has its own version of an authentication manager in the IAuthenticationManager interface which is attached to the HttpContext object.
First, create a new project in Visual Studio of type ASP.NET Web Application (.NET Framework). Name it whatever you want and on the next screen select the MVC template with No Authentication. Note: Okta also works great with Web API; if you’d like to read more on this topic there’s another great blog post about it.
Did you make sure you added the following to your App_Start/Startup.Auth.cs ?
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
}
Check out Brock Allen's primer -
http://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With