Asp.net mvc 在Asp.net MVC中创建自定义主体时,为什么要创建自定义主体接口?
最近我研究了如何创建自定义主体,我得到了答案,但有一件事我不明白,我在堆栈溢出上找到了这个解决方案Asp.net mvc 在Asp.net MVC中创建自定义主体时,为什么要创建自定义主体接口?,asp.net-mvc,iprincipal,principal,custom-authentication,Asp.net Mvc,Iprincipal,Principal,Custom Authentication,最近我研究了如何创建自定义主体,我得到了答案,但有一件事我不明白,我在堆栈溢出上找到了这个解决方案 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Security.Principal; namespace workflow.Authorize { interface ICustomPrincipal : IPrincipal {
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; }
}
}
但我不明白的是,为什么开发人员创建了ICustomPrincipal接口,并强制CustomPrincipal继承它为什么CustomPrincipal类不直接从IPPrincipal接口继承呢接口 开发人员创建了一个自定义界面,并添加了新属性,如FirstName属性和LastName属性,以强制您应用它们 你会发现Iprincipal接口唯一的属性是identity属性,这对你来说还不够 我希望这能帮助您理解为什么开发人员有时会创建一些自定义接口