我正在尝试了解在ASP.NET Core中进行身份验证的正确方法。我看了几个资源(其中大多数已过时)。
有人提供替代性解决方案,说明使用基于云的解决方案(例如Azure AD)或使用IdentityServer4并托管我自己的令牌服务器。
在旧版本的.Net中,身份验证的一种较简单形式是创建自定义原则,并在其中存储其他身份验证用户数据。
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一样进行身份验证,是否仍然可以使用旧方法或是否有较新版本。
使用您自己的令牌服务器和创建自己的自定义原理的利弊是什么?
当使用基于云的解决方案或单独的令牌服务器时,如何将其与当前应用程序集成,我仍将在应用程序中需要一个用户表,如何将两者关联?
由于存在众多不同的解决方案,我如何创建企业应用程序,以允许通过Gmail / Facebook登录,同时仍能够扩展到其他SSO
TL; DR
IdentityServer =通过OAuth 2.0 / OpenId-Connect进行的令牌加密和验证服务
ASP.NET身份= ASP.NET中的当前身份管理策略
我怎样才能像以前版本的.Net一样进行身份验证,是否仍然可以使用旧方法或是否有较新版本。
我认为没有理由无法在ASP.NET Core中实现旧方法,但是总的来说,该策略已被ASP.NET Identity取代,并且ASP.NET Identity在ASP.NET Core中仍然有效。
https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/identity
ASP.NET Identity使用诸如SQL Server之类的后备存储来保存用户信息,例如用户名,密码(散列),电子邮件,电话,并可以轻松地扩展以容纳FirstName,LastName或其他名称。因此,实际上没有理由将用户信息加密为cookie并将其从客户端来回传递到服务器。它支持诸如用户声明,用户令牌,用户角色和外部登录之类的概念。这是ASP.NET Identity中的实体:
使用您自己的令牌服务器和创建自己的自定义原理的利弊是什么?
令牌服务器将是一个生成包含授权和/或认证信息的简单数据结构的系统。授权通常使用名为access_token的令牌的for 。可以这么说,这就是“房子的钥匙”,它使您可以进入门口,进入通常是Web API的受保护资源的住所。对于身份验证,id_token
包含用户/人员的唯一标识符。尽管通常将这样的标识符放在access_token中,但是现在有专用的协议可以这样做:OpenID-Connect。
之所以拥有自己的安全令牌服务(STS),是为了通过加密保护您的信息资产,并控制哪些客户端(应用程序)可以访问这些资源。此外,OpenID-Connect规范中现在存在用于身份控制的标准。IdentityServer是OAuth 2.0授权服务器与OpenID-Connect身份验证服务器结合使用的示例。
但是,如果您只想在应用程序中使用用户表,则不需要这样做。您不需要令牌服务器,只需使用ASP.NET身份。ASP.NET Identity将您的User映射到服务器上的ClaimsIdentity对象-不需要自定义IPrincipal类。
当使用基于云的解决方案或单独的令牌服务器时,如何将其与当前应用程序集成,我仍将在应用程序中需要一个用户表,如何将两者关联?
请参阅以下教程,以将单独的身份解决方案与应用程序集成:https : //identityserver4.readthedocs.io/en/latest/quickstarts/0_overview.html https://auth0.com/docs/quickstart/webapp/aspnet-core
至少需要一个两列表,将用户名映射到外部提供商的用户标识符。这就是ASP.NET Identity中AspNetUserLogins表的功能。但是,该表中的行取决于AspNetUsers中的User记录。
ASP.NET Identity支持外部提供程序,例如Google,Microsoft,Facebook,任何OpenID-Connect提供程序,Azure AD。(Google和Microsoft已经实现了OpenID-Connect协议,因此您也不需要它们的自定义集成程序包,例如该程序包)。此外,ADFS在ASP.NET Core Identity上尚不可用。
请参阅此文档以开始使用ASP.NET Identity中的外部提供程序:
https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/social/
由于存在众多不同的解决方案,我如何创建企业应用程序,以允许通过Gmail / Facebook登录,同时仍能够扩展到其他SSO
如上所述,ASP.NET Identity已经做到了这一点。创建“外部提供程序”表很容易,数据驱动您的外部登录过程。因此,当出现新的“ SSO”时,只需添加一个新行,其中包含提供者的url,客户端ID和它们提供给您的秘密之类的属性。ASP.NET Identity已经在其中的Visual Studio模板中内置了UI,但有关更酷的按钮,请参见“社交登录”。
概要
如果您只需要具有密码登录功能的用户表和用户配置文件,则ASP.NET Identity非常适合。无需外部机构的参与。但是,如果有许多应用程序需要访问许多api,那么保护和验证身份以及访问令牌的独立权限就很有意义。IdentityServer非常适合,请参阅openiddict-core或Auth0以获取云解决方案。
我很抱歉,这没有达到目标或太过入门。请随时进行互动,以找到您想要的靶心。
附录:Cookie身份验证
要使用Cookie进行裸身身份验证,请按照以下步骤操作。但是,据我所知,不支持自定义声明主体。为了获得相同的效果,请使用ClaimPrincipal
对象的Claims列表。
在Visual Studio 2015/2017中创建一个新的ASP.NET Core 1.1 Web应用程序,在对话框中选择“无身份验证”。然后添加包:
Microsoft.AspNetCore.Authentication.Cookies
在适当的Configure
方法下Startup.cs
(在之前app.UseMvc
):
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "MyCookieMiddlewareInstance",
LoginPath = new PathString("/Controller/Login/"),
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
然后构建一个登录ui并将html表单发布到这样的Action方法中:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(String username, String password, String returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
// check user's password hash in database
// retrieve user info
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, username),
new Claim("FirstName", "Alice"),
new Claim("LastName", "Smith")
};
var identity = new ClaimsIdentity(claims, "Password");
var principal = new ClaimsPrincipal(identity);
await HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", principal);
return RedirectToLocal(returnUrl);
}
ModelState.AddModelError(String.Empty, "Invalid login attempt.");
return View();
}
HttpContext.User对象应该具有您的自定义声明,并且可以轻松检索ClaimPrincipal的List集合。
我希望这足够了,因为对于StackOverflow而言,完整的解决方案/项目似乎有点多。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句