当前位置: 首页>>代码示例>>C#>>正文


C# IUserRepository.GetByGitHubId方法代码示例

本文整理汇总了C#中IUserRepository.GetByGitHubId方法的典型用法代码示例。如果您正苦于以下问题:C# IUserRepository.GetByGitHubId方法的具体用法?C# IUserRepository.GetByGitHubId怎么用?C# IUserRepository.GetByGitHubId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IUserRepository的用法示例。


在下文中一共展示了IUserRepository.GetByGitHubId方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Configure

        // Configure is called after ConfigureServices is called.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IUserRepository userRepository, InatorConstraint inatorConstraint)
        {
            loggerFactory.MinimumLevel = LogLevel.Information;
            loggerFactory.AddConsole();
            loggerFactory.AddDebug();

            // Configure the HTTP request pipeline.

            // Add the following to the request pipeline only in development environment.
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage(DatabaseErrorPageOptions.ShowAll);
            }
            else
            {
                // Add Error handling middleware which catches all application specific errors and
                // sends the request to the following path or controller action.
                app.UseExceptionHandler("/Home/Error");
            }

            // Add the platform handler to the request pipeline.
            app.UseIISPlatformHandler();

            // Add static files to the request pipeline.
            app.UseStaticFiles();

            app.UseCookieAuthentication(options => {
                options.AutomaticAuthentication = true;
                //options.AutomaticChallenge = true;
                options.LoginPath = new PathString("/login");
            });

            string githubClientId = this.Configuration["GitHubClientId"];
            string githubClientSecret = this.Configuration["GitHubClientSecret"];

            // Add GitHub Authentication
            // http://www.jerriepelser.com/blog/introduction-to-aspnet5-generic-oauth-provider
            // https://github.com/aspnet/Security/blob/dev/samples/SocialSample/Startup.cs
            app.UseOAuthAuthentication(new OAuthOptions {
                AuthenticationScheme = "GitHub",
                DisplayName = "Github",
                ClientId = githubClientId,
                ClientSecret = githubClientSecret,
                CallbackPath = new PathString("/signin-github"),
                AuthorizationEndpoint = "https://github.com/login/oauth/authorize",
                TokenEndpoint = "https://github.com/login/oauth/access_token",
                SaveTokensAsClaims = false,
                UserInformationEndpoint = "https://api.github.com/user",
                // Retrieving user information is unique to each provider.
                Events = new OAuthEvents {
                    OnCreatingTicket = async context => {
                        // Get the GitHub user

                        var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
                        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
                        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                        var response = await context.Backchannel.SendAsync(request, context.HttpContext.RequestAborted);
                        response.EnsureSuccessStatusCode();

                        var githubUser = JObject.Parse(await response.Content.ReadAsStringAsync());

                        var id = githubUser.Value<int>("id");
                        if (id < 1) {
                            throw new ArgumentNullException("id");
                        }
                        var login = githubUser.Value<string>("login");
                        var name = githubUser.Value<string>("name");
                        var avitarUrl = githubUser.Value<string>("avatar_url");

                        User user = userRepository.GetByGitHubId(id) ?? new User { GitHubId = id };
                        user.Login = login;
                        user.Name = name;
                        user.AvitarUrl = avitarUrl;
                        userRepository.Save(user);

                        context.Identity.AddClaim(new Claim(
                            ClaimTypes.NameIdentifier, user.UserId.ToString()
                        ));

                        if (!string.IsNullOrEmpty(name)) {
                            context.Identity.AddClaim(new Claim(
                                "urn:github:name", name,
                                ClaimValueTypes.String, context.Options.ClaimsIssuer
                            ));
                        }
                        if (!string.IsNullOrEmpty(avitarUrl)) {
                            context.Identity.AddClaim(new Claim(
                                "urn:github:avitar", avitarUrl,
                                ClaimValueTypes.String, context.Options.ClaimsIssuer
                            ));
                        }

                        if (user.IsAdmin) {
                            context.Identity.AddClaim(new Claim(
                                ClaimTypes.Role, "admin"
                            ));
//.........这里部分代码省略.........
开发者ID:robrich,项目名称:api-inator,代码行数:101,代码来源:Startup.cs


注:本文中的IUserRepository.GetByGitHubId方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。