Пример #1
0
 public IActionResult GetListByUsrName(int pageIndex, int rowCount, string userName, string usrType)
 {
     /*判断是否合法*/
     if (ModelState.IsValid)
     {
         try
         {
             if (string.IsNullOrEmpty(usrType))
             {
                 usrType = "2,3";
             }
             IList <HelpCenter.Model.Usr> usrList =
                 HelpCenter.BLL.Usr.GetListByUsrName(pageIndex, rowCount, out int _totalCount, out int _pageCount, userName, usrType,
                                                     User.Identities.First(u => u.IsAuthenticated).FindFirst("UsrDeptId").Value);
             return(null == usrList
                 ? Ok(new { result = false, tips = ResponseMessageTips.MSG_USR_INFO_NO_FOUND })
                 : Ok(new
             {
                 result = true,
                 tips = ResponseMessageTips.MSG_PROCESS_SUCCESS,
                 data = new { totalCount = _totalCount, pageCount = _pageCount, info = usrList }
             }));
         }
         catch (Exception e)
         {
             return(Ok(new { result = false, tips = ResponseMessageTips.MSG_PROCESS_EXCEPTION + e.Message.ToString() }));
         }
     }
     return(Ok(new { result = false, tips = ResponseMessageTips.MSG_PROCESS_DATA_FORMAT_ERROR }));
 }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add EFCore DB Context (in-memory because it is demo-only)
            services.AddDbContext <MachineVisualizerDataContext>(options => options.UseInMemoryDatabase("machines"));

            // Cookie configuration for HTTPS
            services.Configure <CookiePolicyOptions>(options => options.MinimumSameSitePolicy = SameSiteMode.None);

            // Add HTTP client used to get tokens from identity server
            services.AddHttpClient("identity-server", c => c.BaseAddress = new Uri(Configuration["Oidc:Domain"]));

            // Add authentication services see https://identityserver4.readthedocs.io
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultSignInScheme       = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddOpenIdConnect("IdentityServer", options =>
            {
                // Set the authority to your domain
                options.Authority = Configuration["Oidc:Domain"];

                // Configure the Client ID and Client Secret
                options.ClientId     = Configuration["Oidc:ClientId"];
                options.ClientSecret = Configuration["Oidc:ClientSecret"];

                // Set response type to code
                options.ResponseType = OpenIdConnectResponseType.Code;

                // Configure the scope
                options.Scope.Clear();
                options.Scope.Add("openid profile email");

                // Set the callback path
                options.CallbackPath = new PathString("/callback");

                // Configure the Claims Issuer to be Auth0
                options.ClaimsIssuer = Configuration["Oidc:Domain"];

                options.Events = new OpenIdConnectEvents
                {
                    OnAuthorizationCodeReceived = context =>
                    {
                        if (Debugger.IsAttached)
                        {
                            // Do NOT write access codes to logs. This is for training purposes only.
                            Debug.WriteLine($"Received code {context.TokenEndpointRequest.Code}, requesting access token");
                        }

                        return(Task.CompletedTask);
                    },
                    OnTokenValidated = async context =>
                    {
                        if (Debugger.IsAttached)
                        {
                            // Do NOT write access tokens to logs. This is for training purposes only.
                            Debug.WriteLine($"Token {context.TokenEndpointResponse.AccessToken} validated");
                        }

                        // Get profile information and add it to claims
                        using var client = new HttpClient();
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", context.TokenEndpointResponse.AccessToken);
                        var response = await client.GetAsync(new Uri($"{Configuration["Oidc:Domain"]}/connect/userinfo"));
                        response.EnsureSuccessStatusCode();
                        var profile = await response.Content.ReadFromJsonAsync <UserProfile>();
                        context.Principal !.Identities.First().AddClaim(new Claim(ClaimTypes.Email, profile !.Email));
                        context.Principal !.Identities.First().AddClaim(new Claim(ClaimTypes.Name, profile.Name));
                    },
                    OnRedirectToIdentityProviderForSignOut = (context) =>
                    {
                        // End session on identity server
                        context.Response.Redirect($"{Configuration["Oidc:Domain"]}/connect/endsession");
                        context.HandleResponse();

                        return(Task.CompletedTask);
                    }
                };
            });