我正在try 了解在ASP.NET Core中进行身份验证的正确方式.我已经看过几个资源(大多数都过时了).
有些人提供替代解决方案,声明使用基于云的解决方案,如Azure AD,或使用IdentityServer 4并托管我自己的令牌服务器.
在旧版本的.Net的一种更简单的身份验证形式是创建一个自定义的Iprinciple,并在其中存储额外的身份验证用户数据.
public interface ICustomPrincipal : System.Security.Principal.IPrincipal
{
string FirstName { get; set; }
string LastName { get; set; }
}
public class CustomPrincipal : ICustomPrincipal
{
public IIdentity Identity { get; private set; }
public CustomPrincipal(string username)
{
this.Identity = new GenericIdentity(username);
}
public bool IsInRole(string role)
{
return Identity != null && Identity.IsAuthenticated &&
!string.IsNullOrWhiteSpace(role) && Roles.IsUserInRole(Identity.Name, role);
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get { return FirstName + " " + LastName; } }
}
public class CustomPrincipalSerializedModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
然后将数据序列化为cookie,并将其返回给客户端.
public void CreateAuthenticationTicket(string username) {
var authUser = Repository.Find(u => u.Username == username);
CustomPrincipalSerializedModel serializeModel = new CustomPrincipalSerializedModel();
serializeModel.FirstName = authUser.FirstName;
serializeModel.LastName = authUser.LastName;
JavaScriptSerializer serializer = new JavaScriptSerializer();
string userData = serializer.Serialize(serializeModel);
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,username,DateTime.Now,DateTime.Now.AddHours(8),false,userData);
string encTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
Response.Cookies.Add(faCookie);
}
我的问题是:
如何进行与以前版本的类似的身份验证.Net的旧方法是否仍然有效,或者是否有更新的版本.
与创建自己的自定义原则相比,使用您自己的令牌服务器有什么利弊?
当使用基于云的解决方案或单独的令牌服务器时,您将如何将其与您当前的应用程序集成,我的应用程序中是否仍然需要一个USERS表?您如何将两者关联起来?
既然有这么多不同的解决方案,我如何才能创建一个企业应用程序,允许通过Gmail/Facebook登录,同时仍然能够扩展到其他SSO
- 这些技术的一些简单实现是什么?