M
M
Mikhail2016-11-10 14:31:04
ASP.NET
Mikhail, 2016-11-10 14:31:04

Using ASP.NET Identity in a three-tier architecture. How to change CookieAuthenticationProvider?

I use a three-tier architecture and ASP.NET Identity. I used a guide with metanit: metanit.com/sharp/mvc5/23.10.php
The problem is that I would like to add my CookieAuthenticationProvider to UseCookieAuthentication in the Startup mvc project.

Provider = new CookieAuthenticationProvider
                {
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>
                    (TimeSpan.FromMinutes(30), async (manager, user) =>
                    {
                        var userIdentity = await manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
                        //userIdentity.AddClaim(new Claim("FriendlyName", friendlyName));
                        //userIdentity.AddClaim(new Claim("balance", balance.ToString(CultureInfo.CurrentCulture)));
                        //userIdentity.AddClaim(new Claim("sms", sms.ToString()));
                        //userIdentity.AddClaim(new Claim("minutes", minutes.ToString()));
                        return userIdentity;
                    }, id => id.GetUserId<int>())
                }

The problem is that I get error CS0012 C# The type is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.AspNet.Identity.EntityFramework, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
Which can be solved by importing the desired lib, but it will also drag EF along with it. And as I understand it, EF should not be in PL. How to solve this problem differently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail, 2016-11-10
@dmitrievMV

So far, I've solved the problem in a different way.
1) Transferred ApplicationRoleManager, ApplicationSignInManager, ApplicationUserManager from DAL to BLL where they should have been, as I understand it, while leaving UserStore and RoleStore in DAl in UnitOfWork.
2) Created 2 static methods in ApplicationSignInManager

public static Func<CookieValidateIdentityContext, Task> OnValidateIdentity()
        {
            return SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(TimeSpan.FromMinutes(30), GetIdentityAsync, id => id.GetUserId<int>());
        }

        public static async Task<ClaimsIdentity> GetIdentityAsync(ApplicationUserManager manager, ApplicationUser user)
        {
            var userIdentity = await manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
            //userIdentity.AddClaim(new Claim("FriendlyName", friendlyName));
            //userIdentity.AddClaim(new Claim("balance", balance.ToString(CultureInfo.CurrentCulture)));
            //userIdentity.AddClaim(new Claim("sms", sms.ToString()));
            //userIdentity.AddClaim(new Claim("minutes", minutes.ToString()));
            return userIdentity;
        }

and connected Owin libraries in BLL
3) in Startup in MVC and used the created method
Provider = new CookieAuthenticationProvider
                {
                    OnValidateIdentity = ApplicationSignInManager.OnValidateIdentity()
                }

Thus, BLL appeared to work with OWIN, and EF did not appear in PL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question