MVC2 :: How do I * USE * custom IIdentity class?

I am trying to save the entire truck of user information from a web service. Since this is information about an authenticated user, I thought it would make sense to store this information in a IIdentity user implementation.

The user MagicMembershipProvider.GetUser(string id, bool userIsOnline)calls the web service and returns an instance MagicMembershipUserwith all the fields filled in (department, phone number, other employee information).

The user membership provider and user membership user are working fine.

What and where is the best place to put membership user information in an object IPrincipal Useraccessible to each controller?

I'm trying to wrap my brain around the flow of security programs with IIdentity, IPrincipal and Role support in an MVC2 application, but I really struggle here and can use some mentoring. There are Internet Tone articles on the details, but not much on the whole.

Edit

My best guess is to assign HttpContext.Current.Userto FormsAuthenticationService:

public void SignIn(string userName, bool createPersistentCookie)
{
  if (String.IsNullOrEmpty(userName)) 
    throw new ArgumentException("Value cannot be null or empty.", "userName");

  try
  {
    FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
    MagicMembershipUser magicUser = _provider.GetUser("", false) 
      as MagicMembershipUser;
    MagicIdentity identity = new MagicIdentity(userName, magicUser);
    GenericPrincipal principal = new GenericPrincipal(identity, null);

    HttpContext.Current.User = principal;
  }
  catch (Exception)
  {
    throw;
  }

    }
+3
source share
1 answer

What and where is the best place to put membership user information in an IPrincipal User object accessible on each controller?

[Authorize] . AuthorizeCore , .

:

public class MagicAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var isAuthorized = base.AuthorizeCore(httpContext);
        if (isAuthorized)
        {
            var username = httpContext.User.Identity.Name;
            var magicUser = _provider.GetUser(username, false) as MagicMembershipUser;
            var identity = new MagicIdentity(username, magicUser);
            var principal = new GenericPrincipal(identity, null);
            httpContext.User = principal;
        }
        return isAuthorized;
    }
}

, , [MagicAuthorize].

+1

Source: https://habr.com/ru/post/1786800/


All Articles