Mic*_*rds 22 c# authentication asp.net-mvc openid-connect asp.net-core
任何人都可以向我指出一些好的文档或提供有关实现ASP.NET核心REST API的身份验证和授权的最佳方法的良好信息.我需要首先对应用程序进行身份验证和授权,然后对用户进行身份验证和授权.
理想情况下,我希望能够限制经过身份验证的应用和/或用户可以访问的控制器方法.
我正在考虑使用AspNet.Security.OpenIdConnect.Serverenter进行App身份验证,但我不确定如何最好地执行用户身份验证.也许在不同的端点上为具有不同标头的用户重用OpenIdConnect身份验证以包含用户令牌.
一旦通过身份验证,我就会考虑使用角色基本安全来限制可以访问哪些控制器方法.
这是解决这个问题的正确途径吗?
这实际上是一个更难的问题,因为使用api的客户端(软件客户端)的类型似乎驱动了需要什么样的auth*.例如,在Web应用程序中,Web应用程序需要auth*,然后Asp.Net Identity可以使用令牌或cookie.但是,如果其他客户端将使用提供的服务(移动应用程序,WUP应用程序,那么使用令牌身份验证可能更容易实现.当我遇到此问题时,我遇到了一个问题,因为我没有知识差距我真的理解OAuth.我必须回到基础.
https://alexbilbie.com/guide-to-oauth-2-grants/
https://www.pluralsight.com/courses/oauth2-json-web-tokens-openid-connect-introduction
围绕Asp.Net Identity"Seem"的大多数教程都面向Web客户端.虽然有可能找到那些不是.随着asp.net核心的引入,语法已经改变,许多显示组合cookie和令牌认证的旧教程不再适用.此外,Web Api不再与Visual Studio中的其他项目类型分离,使更改更加明显.这是一些较旧的教程.
http://satvasolutions.com/combine-asp-net-identity-web-api-and-mvc-best-in-a-single-web-app/
http://blog.iteedee.com/2014/03/asp-net-identity-2-0-cookie-token-authentication/
IdentityServer是一个完全有效的解决方案,适用于客户端凭据和资源所有者凭据授权(用户,密码),而Brock Allen通常在SO标签下响应很快
/sf/ask/tagged/identityserver4/
或者在标有问题的问题上的github网站上
https://github.com/IdentityServer/IdentityServer4/issues
使用身份服务器,我不得不回到基础并完成教程,以了解这将如何在我的项目中工作.
https://identityserver4.readthedocs.io/en/release/intro/big_picture.html
正如Brock在另一篇文章中快速向我指出的那样,asp.net ef identity是一个用户商店,很适合与资源所有者凭证工作流程一起使用.
对于身份验证,您可以使用ASP.NET Core Identity,它将使用 Microsoft.AspNetCore.Identity.EntityFrameworkCore包,它将使用Entity Framework Core将身份数据和架构持久化到 SQL Server 。
对于授权,您可以使用使用Microsoft.AspNetCore.Authorization包的基于角色的授权。
您还可以查看此视频以了解有关 ASP.NET Core 授权的概述
我找不到任何关于此的好的文档,但是我必须实现相同的目标,因此我通过将标准 ASP.NET 身份验证模板中的操作修改为 REST API 等效项,自己编写了其余 api。
例如,这是我如何执行登录操作的:
// POST: /Account/Login
[HttpPost("[action]")]
[AllowAnonymous]
public async Task<ReturnValue<ApplicationUser>> Login([FromBody] loginModel login)
{
if (ModelState.IsValid)
{
ApplicationUser user = await _userManager.FindByEmailAsync(login.email);
if (user == null)
{
return new ReturnValue<ApplicationUser>(false, "Login failed, check username and password.", null);
}
// else if (user.EmailConfirmed == false)
// {
// return new ReturnValue<ApplicationUser>(true, "Confirm email address.", null, user);
// }
else
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(user, login.password, (bool)login.rememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
return new ReturnValue<ApplicationUser>(true, user);
}
//if (result.RequiresTwoFactor)
//{
// return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
//}
if (result.IsLockedOut)
{
return new ReturnValue<ApplicationUser>(false, "The account is locked out.", null);
}
}
}
else
{
string message = string.Join("; ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));
return new ReturnValue<ApplicationUser>(false, "Invalid login attempt: " + message, null);
}
// If we got this far, something failed in the model.
return new ReturnValue<ApplicationUser>(false, "Login failed.", null);
}
Run Code Online (Sandbox Code Playgroud)
如果您从浏览器中的 javascript 调用 API,则将加载 cookie,并且您应该能够对 API 进行进一步的授权调用,如果您从其他类型的客户端调用,则需要确保保留 CookieContainer用于授权呼叫。
从现在起,您可以通过标准 Microsoft 库使用 [Authorize] 装饰器来授权您的 REST API 控制器: https: //learn.microsoft.com/en-us/aspnet/core/security/authentication/identity
祝你好运。
| 归档时间: |
|
| 查看次数: |
5441 次 |
| 最近记录: |