Web Api 2 Basic Auth Generic Principal not installed

I have the following code to set up a general principal.

public class AuthenticationHandler: DelegatingHandler
{
    protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                                                  CancellationToken cancellationToken)
    {
        var accessToken = request.Headers.Authorization;
        if (accessToken == null)
            return base.SendAsync(request, cancellationToken);

        // Catch an error with regards to the accessToken being invalid
        try
        {
            var formsAuthenticationTicket = FormsAuthentication.Decrypt(accessToken.Parameter);

            if (formsAuthenticationTicket == null)
                return base.SendAsync(request, cancellationToken);

            var data = formsAuthenticationTicket.UserData;
            var userData = JsonConvert.DeserializeObject<LoginRoleViewModel>(data);

            var identity = new GenericIdentity(userData.Id.ToString(), "Basic");

            var userRole = userData.Roles.ToArray();
            var principal = new GenericPrincipal(identity, userRole);
            Thread.CurrentPrincipal = principal;
            HttpContext.Current.User = principal;

        }
        catch (Exception ex)
        {
            var responseMessage = request.CreateResponse(HttpStatusCode.BadRequest, new { ex.Message }); // return ex for full stacktrace
            return Task<HttpResponseMessage>.Factory.StartNew(() => responseMessage);
        }

        return base.SendAsync(request, cancellationToken);
    }

}

Below is an example of a controller

[Authorize(Roles = "Administrator, Customers")]
[HttpGet("customers/{id}")]
public CustomerViewModel GetCustomer(string id)
{
    var param = AuthService.CheckPermission(Request, User, id);
    var customer = Db.Customers.Find(param);
    return Mapper.Map<CustomerViewModel>(customer);
}

And this is where I check if user roles are

public int CheckPermission(HttpRequestMessage request, IPrincipal user, string param)
{
    if (user.IsInRole("Customers") || user.IsInRole("Dealerships"))
    {
        if (param == null || param != "me")
            throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.Forbidden, "unauthorized request"));
        param = user.Identity.Name;
    }

    return Convert.ToInt32(param);
}

Did this work fine before upgrading to Web Api 2 and MVC 5? Now the User has no roles or identity, something has changed, which I do not know about?

+4
source share
4 answers

Not sure why it doesn't work anymore, but in Web API 2 there is a new class with a property , and this is what you need to set for the update . You can access the context object from the request. HttpRequestContext Principal Principal

+8
source

, .

request.GetRequestContext().Principal = new GenericIdentity(userData.Id.ToString(), "Basic");

, IMO ,

HttpContext.Current.User = new GenericIdentity(userData.Id.ToString(), "Basic");
+5

, , . , .

var identity = new GenericIdentity("ApiUser", request.Headers.Authorization.Scheme);
var principal = new GenericPrincipal(identity, new string[0]);                            
request.GetRequestContext().Principal = principal;
+2

"Basic" "Forms"

var identity = new GenericIdentity(userData.Id.ToString(), "Forms");

, FormsAuthenticationModule? MVC5 . http://msdn.microsoft.com/en-us/library/system.web.security.formsauthenticationmodule.aspx

FormsAuthentication_OnAuthenticate .

http://blogs.msdn.com/b/webdev/archive/2013/07/03/understanding-owin-forms-authentication-in-mvc-5.aspx

0

All Articles