How to assign HttpContext.User value in ASP.NET MVC?

I am writing a controller as shown below:

public class AccountController : Controller
{
    public ActionResult Login(/*---*/)
    {
        GenericIdentity identity = new GenericIdentity("userName");
        GenericPrincipal principal = new GenericPrincipal(identity, new string[] { "role1", "role2" });
        this.HttpContext.User = principal;
        /*---*/;
    }
}

After logging in, I can get the user name User.Identity.Name in another controller. But User.IsInRole ("role1") always returns false.

How can I assign a value to a user, I don’t want to use membership ...

+3
source share
2 answers

Hm.

Using membership?

At least the lower level API. You must designate him as a director in some event (which basically turns into a cookie and deserializes with every call).

Details are at http://support.microsoft.com/kb/306590

http://msdn.microsoft.com/en-us/library/aa302399.aspx

+1

-, . cookie. IPrincipal. Application_AuthenticateRequest Global.ascx,

MVC - / , , , .

+5

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


All Articles