Identity 2.0 is zero in MVC 5 Initializer

I have an MVC 5 project using Asp.Net Identity 2.0. I also use a generic repository template. As part of the database schema, I have fields for various tables that store the user ID for inserting / updating / deleting the user. Therefore, I would like to pass the user object or user ID, at least, to the shared repository that will be used when changing entries.

However, since I could not access the identifier directly in the repository class, I am trying to pass it on to create the repository. It looks like this:

using System.Data.Entity;
using System.Threading.Tasks;
using System.Net;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using My.Models;

namespace My.Controllers
{
    [Authorize]
    public class FooController : MasterController
    {
        private IGenericRepositoryAsync<Topic> _repository;

        public FooController()
        {
            //Point A
            _repository = new GenericRepositoryAsync<Foo>(User.Identity);
        }


        public async Foo<ActionResult> Index()
        {
            //POINT B
            //_repository = new GenericRepositoryAsync<Foo>(User.Identity);
            return View(await _repository.GetAllAsync());
        }
    }
}

" A" User.Identity null, //Point B null, , Action.

.

+4
2

Web Api

, userId :

public class MyAuthorizeAttribute : AuthorizeAttribute 
{
    public override async Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
    {
        if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()) return;

        base.OnAuthorization(actionContext);

        Guid userId;

        if (actionContext.RequestContext.Principal.Identity.IsAuthenticated
            && Guid.TryParse(actionContext.RequestContext.Principal.Identity.GetUserId(), out userId))
        {
            actionContext.Request.Properties.Add("userId", actionContext.RequestContext.Principal.Identity.GetUserId());
        }
    }
}

, Application Startup (Global.asax web_start, Startup.cs) :

config.Filters.Add(new MyAuthorizeAttribute());

Owin Startup.cs - :

public void Configuration(IAppBuilder app)
{
    ConfigureOAuth(app);

    HttpConfiguration config = new HttpConfiguration();
    WebApiConfig.Register(config);
    config.Filters.Add(new MyAuthorizeAttribute());
}

userId jus:

Guid userId = (Guid) ActionContext.Request.Properties["userId"];

MVC

MVC :

using Microsoft.AspNet.Identity;

...

User.Identity.GetUserId();

, Web Api:

public override void OnAuthorization(AuthorizationContext filterContext)
{
    ...
    Guid userId = filterContext.HttpContext.User.Identity.Name.GetUserId();
    ...
}
+3

MVC, GetUserId OnAuthentication(), :

protected override void OnAuthentication(System.Web.Mvc.Filters.AuthenticationContext filterContext) {
      Repo = new ReposWrapper();

      try {
         Repo.CurrentUserId = User.Identity.GetUserId();
      } catch (NullReferenceException) {}

      base.OnAuthentication(filterContext);
}
-1

All Articles