zoukankan      html  css  js  c++  java
  • .net core 登陆认证

    1:startup:

     services.AddAuthentication(IdentityService.AuthenticationScheme)
                     .AddCookie(IdentityService.AuthenticationScheme, options =>
                     {
                         options.AccessDeniedPath = "/Account/Login/";
                         options.LoginPath = "/Account/Login/";
                         //options.LogoutPath = new PathString("/Account/Logout");
                         options.Cookie.Domain = Configuration["CookieDomain"];
                     });
                //自定义秘钥加密
                services.AddDataProtection().DisableAutomaticKeyGeneration()
                .PersistKeysToFileSystem(new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ShareKeys")))
                .SetApplicationName("Jst.LeYou");
                services.AddScoped<PermissionFilter>();

    sharekeys

    <?xml version="1.0" encoding="utf-8"?>
    <key id="91732fd5-4ec5-447f-9c6f-c832bda18354" version="1">
      <creationDate>2018-09-04T01:56:26.1864522Z</creationDate>
      <activationDate>2018-09-04T01:56:26.1729285Z</activationDate>
      <expirationDate>2118-09-04T01:56:26.1729285Z</expirationDate>
      <descriptor deserializerType="Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60">
        <descriptor>
          <encryption algorithm="AES_256_CBC" />
          <validation algorithm="HMACSHA256" />
          <masterKey p4:requiresEncryption="true" xmlns:p4="http://schemas.asp.net/2015/03/dataProtection">
            <!-- Warning: the key below is in an unencrypted form. -->
            <value></value>
          </masterKey>
        </descriptor>
      </descriptor>
    </key>
    // 创建用户成功后,把用户信息存在 calm中           
    HttpContext.SignInAsync(IdentityService.AuthenticationScheme, user);
        public class PermissionFilter : ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext context)
            {
                if(IsNoLogin(context))
                {
                    base.OnActionExecuting(context);
                    return;
                }
    
                if (!context.HttpContext.User.Identity.IsAuthenticated)
                {
                    if (IsAjax(context))
                    {
                        context.Result = new JsonResult(new { Success = false, Message = "您没有权限执行此操作!" });
                        return;
                    }
                    else
                    {
                        context.Result = new RedirectResult("/Account/Login");
                        return;
                    }
                }
            
                base.OnActionExecuting(context);
            }
        }
  • 相关阅读:
    yzoj P2344 斯卡布罗集市 题解
    yzoj P2350 逃离洞穴 题解
    yzoj P2349 取数 题解
    JXOI 2017 颜色 题解
    NOIP 2009 最优贸易 题解
    CH 4302 Interval GCD 题解
    CH4301 Can you answer on these queries III 题解
    Luogu2533[AHOI2012]信号塔
    Luogu3320[SDOI2015]寻宝游戏
    Luogu3187[HNOI2007]最小矩形覆盖
  • 原文地址:https://www.cnblogs.com/gavinhuang/p/9619229.html
Copyright © 2011-2022 走看看