OWIN - Customizing UserManager

12,536

Solution 1

Try changing this line.

 var claimsIdentity = new ClaimsIdentity(claimCollection, "Company Portal");

To this

var claimsIdentity = new ClaimsIdentity(claimCollection, DefaultAuthenticationTypes.ApplicationCookie);

That should generate your cookie for you that is needed.

Solution 2

The UserManager manages the user identity in the database as well as validating credentials. In short, it's a DB lookup tool. To get the user "logged into" your app, you need to issue some sort of token (like a cookie for browser apps, or a token for api apps). The most recent approach in ASP.NET is with the Cookie Authentication Middleware for browser apps. See here for more info on the cookie middleware:

http://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/

Solution 3

Oracle Data Provider for .NET currently does not support Asynchronous Query and Save.

Solution 4

Looking at the SignIn method created by an ASP.NET MVC 5 default project we can see this code:

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

What we can notice is that AuthenticationManager wich is one who takes care of autenthication sign in, after we get the identity also is needed to SignIn with the AuthenticationManager. So maybe your problem is not with UserManager.

The AuthenticationManager instance in the Controller class is retrieved by this code:

private IAuthenticationManager AuthenticationManager
{
    get
    {
        return HttpContext.GetOwinContext().Authentication;
    }
}
Share:
12,536
Leonel Sanches da Silva
Author by

Leonel Sanches da Silva

Programmer, Systems Analyst, IT Architect and Entrepreneur from Curitiba, Brazil, living in Irvine, California. Bachelor in Computing Science from Federal University of Paraná since 2007, acting in IT since 1998 as a hobbist. Microsoft MVC 2017-2018. I own a company named Design Líquido (Liquid Design, in english), which provides services in Graphical Design and IT (http://www.designliquido.com.br), and I helped to found a T-Shirt and Clothing online store (http://doppelstore.com.br) and a Benefits company (https://useswood.com), and more companies should be on inception shortly. Additionally, I'm a professional tutor, specialized in professional disciplines for IT professionals in Brazil: http://codingcraft.com.br. GitHub: https://github.com/leonelsanchesdasilva NuGet: https://www.nuget.org/profiles/cigano/ Liked my answers? Did I help you? Buy me a beer (or, in Portuguese, me paga uma bera).

Updated on June 11, 2022

Comments

  • Leonel Sanches da Silva
    Leonel Sanches da Silva about 2 years

    I had to customize the UserManager class to find and authenticate users in the company structure (mixes Active Directory Authentication with another Oracle Authetication). Though I have implemented the FindAsync and CreateIdentityAsync, the user is not set as authenticated.

    My UserManager implementation:

    using System;
    using System.Collections.Generic;
    using System.Dynamic;
    using System.Security.Claims;
    using System.Web;
    using MyProject.Common;
    using MyProject.Models;
    using Microsoft.AspNet.Identity;
    using System.Threading.Tasks;
    
    namespace MyProject.Infrastructure
    {
        public class GNUserManager : UserManager<ApplicationUser>
        {
            public GNUserManager(IUserStore<ApplicationUser> store) : base(store)
            {
    
            }        
    
            public override async Task<ApplicationUser> FindAsync(string userName, string password)
            {
                /* Performs some logic here that returns true */
    
                if (foundUser) {
                    return await Task.Run(() => new ApplicationUser
                    {
                        UserName = userName, 
                        Id = userName
                    });
                }
    
                throw new Exception("User not found.");
            }
    
            public override async Task<ClaimsIdentity> CreateIdentityAsync(ApplicationUser user, string authenticationType)
            {
                IList<Claim> claimCollection = new List<Claim>
                {
                    new Claim(ClaimTypes.Name, user.UserName),
                    new Claim(ClaimTypes.Country, "Brazil"),
                    new Claim(ClaimTypes.Email, user.UserName)
                };
    
                var claimsIdentity = new ClaimsIdentity(claimCollection, "Company Portal");
    
                return await Task.Run(() => claimsIdentity);  
            }
        }
    }
    

    What is lacking to have my user authenticated?

  • Leonel Sanches da Silva
    Leonel Sanches da Silva over 10 years
    I saw this site yesterday, but it's only a glance about what I'm looking for. That's exactly the cookie generation that interests me.
  • Leonel Sanches da Silva
    Leonel Sanches da Silva over 10 years
    This line: AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); executes normally, but no cookie is generated. Debugging this code I dive into public override async Task<ClaimsIdentity> CreateIdentityAsync(ApplicationUser user, string authenticationType). What I need to find out is how to generate the cookie.
  • iuristona
    iuristona over 10 years
    Maybe can you call the base Method wich one you are overriding?
  • iuristona
    iuristona over 10 years
    I am on chat now... check there