首页 > 其他分享 >net-core认证和授权

net-core认证和授权

时间:2023-05-23 21:44:12浏览次数:38  
标签:core null await 认证 new net scheme throw principal

public virtual bool IsAuthenticated
        {
            get { return !string.IsNullOrEmpty(_authenticationType); }
        }

登录代码:

   IList<Claim> calims = new List<Claim>();
    calims.Add(new Claim("name","ybd"));
    ClaimsIdentity identity = new ClaimsIdentity(calims);
    var claimsPrincipal = new ClaimsPrincipal(identity); // 这里要加type
    await ctx.SignInAsync("ybd-cookie",claimsPrincipal);

AuthenticationService

/// <summary>
/// Sign a principal in for the specified authentication scheme.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/>.</param>
/// <param name="scheme">The name of the authentication scheme.</param>
/// <param name="principal">The <see cref="ClaimsPrincipal"/> to sign in.</param>
/// <param name="properties">The <see cref="AuthenticationProperties"/>.</param>
/// <returns>A task.</returns>
public virtual async Task SignInAsync(HttpContext context, string? scheme, ClaimsPrincipal principal, AuthenticationProperties? properties)
{
    if (principal == null)
    {
        throw new ArgumentNullException(nameof(principal));
    }
 
    if (Options.RequireAuthenticatedSignIn)
    {
        if (principal.Identity == null)
        {
            throw new InvalidOperationException("SignInAsync when principal.Identity == null is not allowed when AuthenticationOptions.RequireAuthenticatedSignIn is true.");
        }
        if (!principal.Identity.IsAuthenticated)
        {
            throw new InvalidOperationException("SignInAsync when principal.Identity.IsAuthenticated is false is not allowed when AuthenticationOptions.RequireAuthenticatedSignIn is true.");
        }
    }
 
    if (scheme == null)
    {
        var defaultScheme = await Schemes.GetDefaultSignInSchemeAsync();
        scheme = defaultScheme?.Name;
        if (scheme == null)
        {
            throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultSignInScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).");
        }
    }
 
    // 关键代码  获得身份验证处理程序
    var handler = await Handlers.GetHandlerAsync(context, scheme);
    if (handler == null)
    {
        throw await CreateMissingSignInHandlerException(scheme);
    }
 
    var signInHandler = handler as IAuthenticationSignInHandler;
    if (signInHandler == null)
    {
        throw await CreateMismatchedSignInHandlerException(scheme, handler);
    }
 
    await signInHandler.SignInAsync(principal, properties);
}

 

标签:core,null,await,认证,new,net,scheme,throw,principal
From: https://www.cnblogs.com/Insist-Y/p/17426493.html

相关文章

  • Paper Reading: forgeNet a graph deep neural network model using tree-based ensem
    目录研究动机文章贡献本文方法图嵌入深度前馈网络forgeNet特征重要性评估具体实现模拟实验合成数据生成实验评估实验结果真实数据应用BRCA数据集microRNA数据Healthyhumanmetabolomics数据集优点和创新点PaperReading是从个人角度进行的一些总结分享,受到个人关注点的侧重......
  • Kubernetes(k8s)最大启动时长研究
    一、前言应用部署在Kubernetes(k8s)上,有些应用启动慢一些,没启动好就又被k8s重启了二、处理过程1.看日志[2023-05-2314:38:52.249]|-INFO|-[background-preinit]|-o.h.v.i.u.Version[0]|-[TID:N/A]|-HV000001:HibernateValidator6.1.7.Final[2023-05-2314:40:11.817]|-......
  • .Net使用HttpClient以multipart/form-data形式post上传文件及其相关参数
    httpClient模仿Postman的form-data格式 api接口[HttpPost(Name="/Test/FileTest")]publicJGResponse<PutFileByNameResponse>FileTest([FromForm]PutFileByNameRequestrequestDto){varcapthch=GetCaptcha();......
  • Asp.Net中的Fillter
    Asp.net中一共有五种FillterAuthorizationFilter授权过滤器IResourceFilter资源管理过滤器IActionFilter行为过滤器IExceptionFilter异常过滤器IResultFilter结果过滤器publicclassCtmActionFilterAttribute:Attribute,IActionFilter{publicvo......
  • 2步轻松实现ASP.NET Core托管服务执行定时任务
    最近接到一个新项目,需要在项目里添加一个后台任务,定时去发邮件通知客户;由于是一个比较小型的项目,不希望引入Quartz.Net、Hangfire等太重的框架,同时也没持久化要;寻觅了一下发现ASP.NETCore本身带有托管服务,可以执行定时任务。ASP.NETCore提供了IHostedService接口,它使我们能够创......
  • .NET中SQL Server数据库连接方法
    1. 使用本机上的SQLServerExpress实例上的用户实例。     用户实例的连接创建了一个新的SQLServer实例。此连接只能是在本地SQLServer2005实例上并且是通过命名管的windows验证连接才有效。目的就是为了给用户创建一个完全权限的SqlServer实例和有限的计算机管理员......
  • .net6中数据库查询报错:'OFFSET' 附近有语法错误。 在 FETCH 语句中选项 NEXT 的用法无
    错误语句:在数据库查询中使用skip()问题原因:数据库版本为SQLServer2008,不支持'Fetch'和'Next'语句SQLServer2012及后续版本才支持相关语句解决方法:1.引用包: System.Data.SqlClient和EntityFrameworkCore.UseRowNumberForPaging2.使用:在Program.cs中调整数据库连接bu......
  • AKSK加密认证
    AK/SK加密认证AK/SK概述1.什么是AKSKak/sk是一种身份认证方式,常用于系统间接口调用时的身份验证,其中ak为AccessKeyID,sk为SecretAccessKey。客户端和服务端两者会协商保存一份相同的sk,其中sk必须保密。2.AK/SK认证过程客户端在调用的服务端接口时候,会带上ak以及signatu......
  • .Net6自定义拦截器
    .Net6自定义拦截器拦截器是Aop(面向切面编程)的思想指的是不改变原代码封装的前提下去实现更多功能这里通过.net的特性(给一个目标对象添加一段配置信息)的方式去实现拦截器功能新建一个特性namespaceCorePolly{publicclassTestAttribute:Attribute{publ......
  • kubernetes部署Open-LDAP、Go-admin-ldap
    1.搭建openLDAP1.1.创建命名空间kubectlcreatenamespacekube-ops1.2.创建pvc存储使用的是nfs方式挂载,storageClassName为默认,所以可写可不写。mkdir-p~/ldap;cd~/ldapcat>pvc.yaml<<EOFapiVersion:v1kind:PersistentVolumeClaimmetadata:name:ldap-dat......