zoukankan      html  css  js  c++  java
  • asp.net Identity 设置自定义登录

    添加Startup.Auth.cs###

        public partial class Startup
        {
            // For more information on configuring authentication, please visit https://go.microsoft.com/fwlink/?LinkId=301864
            public void ConfigureAuth(IAppBuilder app)
            {
                // Enable the application to use a cookie to store information for the signed in user
                // and to use a cookie to temporarily store information about a user logging in with a third party login provider
                // Configure the sign in cookie
              app.UseCookieAuthentication(new CookieAuthenticationOptions
              {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                ExpireTimeSpan = TimeSpan.FromDays(3)
              });        
                app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    
                // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
                app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
    
                // Enables the application to remember the second login verification factor such as phone or email.
                // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
                // This is similar to the RememberMe option when you log in.
                app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
            }
        }
    

    添加Startup.cs###

    注意要添加[assembly: OwinStartup(typeof(Startup))]

    [assembly: OwinStartup(typeof(Startup))]
    namespace testOwin
    {
      public partial class Startup
      {
        public void Configuration(IAppBuilder app)
        {
          ConfigureAuth(app);
        }
      }
    }
    

    在代码中就可以自己设置登录状态了###

        private ClaimsIdentity ClaimsIdentity(string userName)
        {
          //string[] userRoles = (string[])Session["UserRoles"];
    
          ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
    
          identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userName));
    
          //userRoles.ToList().ForEach((role) => identity.AddClaim(new Claim(ClaimTypes.Role, role)));
    
          identity.AddClaim(new Claim(ClaimTypes.Name, userName));
    
          //AuthenticationManager.SignIn(identity);
          AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity);
          return identity;
        }
    
        private IAuthenticationManager AuthenticationManager
        {
          get
          {
            return HttpContext.GetOwinContext().Authentication;
          }
        }
    
  • 相关阅读:
    linux定时执行任务crontab命令用法
    windows下安装python和依赖包的利器——Anaconda
    Python学习笔记1——Python基础
    Mysql创建新用户后无法登录,提示 Access denied for user 'username'@'localhost' (using password: YES)
    Hadoop伪分布搭建
    Hadoop2.20集群搭建
    对于C++中const & T operator= 的一点思考
    搬家啦~
    利用raspberry pi搭建typecho笔记(三) typecho nginx sqlite FAQ
    利用raspberry pi搭建typecho笔记(二) sqlite和typecho部署
  • 原文地址:https://www.cnblogs.com/dupeng0811/p/how-to-set-timeout-for-owincontext-in-mvc-5.html
Copyright © 2011-2022 走看看