Security Cookies login authentication core Detailed

using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.DependencyInjection;

namespace CookieSessionSample
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            // This can be removed after https://github.com/aspnet/IISIntegration/issues/371
            services.AddAuthentication(options =>
            {
                = CookieAuthenticationDefaults.AuthenticationScheme options.DefaultAuthenticateScheme; 
                options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
            }) addCookie (= O> = o.SessionStore new new MemoryCacheTicketStore ());. 
        } 

        public void the Configure (IApplicationBuilder App) 
        { 
            app.UseAuthentication (); 

            App .run (the async context => 
            { 
                IF (! context.User.Identities.Any (Identity => identity.IsAuthenticated)) 
                {// normal authentication process is not written here, it is meant herein the verification found unregistered, 
                // generally by custom user password verification operation Once verified, build under the login credentials 
                    // Make a large identity 
                    var claims = new List <Claim> (1001);
                    claims.Add(new Claim(ClaimTypes.Name, "bob"));
                    for (int i = 0; i < 1000; i++)
                    {
                        claims.Add(new Claim(ClaimTypes.Role, "SomeRandomGroup" + i, ClaimValueTypes.String, "IssuedByBob", "OriginalIssuerJoe"));
                    }

                    //写入登录验证方案与凭证到Cookies
                    await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                        new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme)));   

                    context.Response.ContentType = "text/plain";
                    await context.Response.WriteAsync("Hello First timer");
                    return;
                }

                context.Response.ContentType = "text/plain";
                await context.Response.WriteAsync("Hello old timer");
            });
        }
    }
}

Guess you like

Origin www.cnblogs.com/ms_senda/p/12501157.html