locked
Asp.net MVC Authorization RRS feed

  • Question

  • User1209142922 posted

    Hi Guys,

    I have to deal with an existing MVC4 application's authorization code. The code is relying on Asp.net IPrincipal and the class goes like this

    public class CustomPrincipal :IPrincipal
    {
    	public Identity{get;set;}
    	public CustomPrincipalModel CustomPrincipalModel
    	{
    	}
    
            public bool IsInRole()
            {
    	//Authorization code logic comes here
    	}
    
    }
    
    [Serializable]
    public class CustomPrincipalModel
    {
    	public int id{get;set;}
    	public int age{get;set;}
    	public string FirstName{get;set;}
    	public string LastName{get;set;}
    	public string[] AccessLevels{get;set;}
            public string[] Permissions{get;set;}
    }
    


    Just an overview about the above. The class for Authorization functionality is given above. On Login() function  the "CustomPrincipalModel" is set with the required values, ie the roles and permissions and then encrypt it using FormsAuthentication.Encrypt and stored as userdata along with authorization cookie. The code is working fine. The IsRole() function checks the user authorization on each request from the HttpContext -> CustomPrincipalModel accesslevels and permissions.

    We have a new requirement

    1:To store new levels of information in CustomPrincipalModel. When we added those information as properties the Authorization Cookie exceeded 4096 limit. This is an issue. We are thinking to store the session, ie the custom principal in Redis Cache in azure. Please share your thoughts on this?

    2: We have another requirement to integrate other types of authentications like Google, Twitter, Facebook etc along in this IPrincipal. Right now the login screen is having just username/password field , authenticated against a database and then Authorization Cookie is set. Incase if we are integrating other forms of authentication, what is the recommended approach in this scenario. Could you please share your thoughts on this. ?

    3: We are storing the Access Levels and Permissions as just pure strings: eg: Read, Write, Recursive and so on. Any other good types to handle user permissions?

    Tuesday, August 2, 2016 5:12 PM

Answers

  • User-2057865890 posted

    Hi Vishnu.vg1984,

    1:To store new levels of information in CustomPrincipalModel. When we added those information as properties the Authorization Cookie exceeded 4096 limit. This is an issue. We are thinking to store the session, ie the custom principal in Redis Cache in azure. Please share your thoughts on this?

    If you need to use Redis Cache in Azure, you could start from How to Use Azure Redis Cache. This guide shows you how to get started using Azure Redis Cache.

    2: We have another requirement to integrate other types of authentications like Google, Twitter, Facebook etc along in this IPrincipal. Right now the login screen is having just username/password field , authenticated against a database and then Authorization Cookie is set. Incase if we are integrating other forms of authentication, what is the recommended approach in this scenario. Could you please share your thoughts on this. ?

    About external authentication, you could try using OAuth 2.0 with credentials.

    reference: http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

    3: We are storing the Access Levels and Permissions as just pure strings: eg: Read, Write, Recursive and so on. Any other good types to handle user permissions?

    You could use database table modelling.

    reference: http://www.codeproject.com/Articles/875547/Custom-Roles-Based-Access-Control-RBAC-in-ASP-NET

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 3, 2016 10:00 AM