Return user role for authentication with ASP.NET WebApi

We are following this excellent tutorial using ASP.NET web application and separate user accounts.

To authenticate and obtain a token carrier, follow these steps:

Request URL:http://localhost:3067/token
grant_type=password&username=alice&password=password123

As expected, the token provider returns this:

{
   "access_token":"rkg5dP_Lyg ... TIHLD2xIRJ",
   "token_type":"bearer",
   "expires_in":1209599,
   "userName":"Alice",
   ".issued":"Mon, 03 Feb 2014 19:06:32 GMT",
   ".expires":"Mon, 17 Feb 2014 19:06:32 GMT"
}

It's good. Now, how to add a role property to a JSON response?

   "userRole":"admin"
+3
source share
1 answer

If you use the default project template in Visual Studio, all you need to do is add userRole to your AuthenticationProperties array, which is passed when Authentication.SignIn is called.

, ApplicationOAuthProvider, userRole CreateProperties, :

public static AuthenticationProperties CreateProperties(string userName, string userRole)
{
    IDictionary<string, string> data = new Dictionary<string, string>
    {
        { "userName", userName },
        { "userRole", userRole }
    };
    return new AuthenticationProperties(data);
}

, Authentication.SignIn, , userRole. Token GrantResourceOwnerCrentials ApplicationOAuthProvider, cookie AccountController GetExternalLogin.

+5

All Articles