Why to create a custom principal interface when you want to create a Custom Principal in Asp.net MVC?

363 views Asked by At

Recently I seaerched for how to create custom principal and I got the answer but there is one thing that I do not understand, I found this solution on stack overflow

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;
namespace workflow.Authorize
{
    interface ICustomPrincipal : IPrincipal
    {
        int Id { get; set; }
        string FirstName { get; set; }
        string LastName { get; set; }
    }

    public class CustomPrincipal : ICustomPrincipal
    {

        public IIdentity Identity { get; private set; }
        public bool IsInRole(string role) {

            if(this.Roles.Contains(role))
             return true; 
            else
                return false;
        }

        public CustomPrincipal(string email)
        {
            this.Identity = new GenericIdentity(email);
        }

        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Roles { get; set; }
    }

    public class CustomPrincipalSerializeModel
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Roles { get; set; }
    }

}

but what I do not understand is why the developer created the ICustomPrincipal interface and force the CustomPrincipal to inherit it why not the CustomPrincipal class inherits directly from the IPrincipal interface

1

There are 1 answers

0
AudioBubble On BEST ANSWER

For me if you need to force the users to override additional properties and methods then you have to create a custom interface.

The developer created a custom interface and added new properties like the FirstName property and the LastName property to force you to apply them.

Look here and you will find that the only property that the Iprincipal interface has is the identity property and that could not be enough for you.

I hope that helps you to understand why the developers sometimes create some custom interfaces.