导航

12-004 Identity 之 SignInManager

Posted on 2015-04-24 15:23  DotNet1010  阅读(529)  评论(0)    收藏  举报

--

     public virtual async Task SignInAsync(TUser user, bool isPersistent, string authenticationMethod = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var userIdentity = await CreateUserIdentityAsync(user);
            if (authenticationMethod != null)
            {
                userIdentity.AddClaim(new Claim(ClaimTypes.AuthenticationMethod, authenticationMethod));
            }
            Context.Response.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, userIdentity);
        }

 

      public virtual void SignOut()
        {
            Context.Response.SignOut(IdentityOptions.ApplicationCookieAuthenticationType);
            Context.Response.SignOut(IdentityOptions.ExternalCookieAuthenticationType);
            Context.Response.SignOut(IdentityOptions.TwoFactorUserIdCookieAuthenticationType);
        }

   await Context.AuthenticateAsync(IdentityOptions.TwoFactorRememberMeCookieAuthenticationType);

     public async Task<bool> IsTwoFactorClientRememberedAsync(TUser user,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var userId = await UserManager.GetUserIdAsync(user, cancellationToken);
            var result =
                await Context.AuthenticateAsync(IdentityOptions.TwoFactorRememberMeCookieAuthenticationType);
            return (result != null && result.Identity != null && result.Identity.Name == userId);
        }

 await Context.AuthenticateAsync(IdentityOptions.ExternalCookieAuthenticationType);

    public virtual async Task<ExternalLoginInfo> GetExternalLoginInfoAsync(string expectedXsrf = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var auth = await Context.AuthenticateAsync(IdentityOptions.ExternalCookieAuthenticationType);
            if (auth == null || auth.Identity == null || auth.Properties.Dictionary == null || !auth.Properties.Dictionary.ContainsKey(LoginProviderKey))
            {
                return null;
            }

            if (expectedXsrf != null)
            {
                if (!auth.Properties.Dictionary.ContainsKey(XsrfKey))
                {
                    return null;
                }
                var userId = auth.Properties.Dictionary[XsrfKey] as string;
                if (userId != expectedXsrf)
                {
                    return null;
                }
            }

            var providerKey = auth.Identity.FindFirstValue(ClaimTypes.NameIdentifier);
            var provider = auth.Properties.Dictionary[LoginProviderKey] as string;
            if (providerKey == null || provider == null)
            {
                return null;
            }
            return new ExternalLoginInfo(auth.Identity, provider, providerKey, auth.Description.Caption);
        }

  await Context.AuthenticateAsync(IdentityOptions.TwoFactorUserIdCookieAuthenticationType);

     private async Task<TwoFactorAuthenticationInfo> RetrieveTwoFactorInfoAsync(CancellationToken cancellationToken)
        {
            var result = await Context.AuthenticateAsync(IdentityOptions.TwoFactorUserIdCookieAuthenticationType);
            if (result != null && result.Identity != null)
            {
                return new TwoFactorAuthenticationInfo
                {
                    UserId = result.Identity.Name,
                    LoginProvider = result.Identity.FindFirstValue(ClaimTypes.AuthenticationMethod)
                };
            }
            return null;
        }