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


C# IAuthenticationService.IsAuthenticated方法代码示例

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


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

示例1: SendAsync

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // Message handler is created on application start, not per request, so we need to resolve the authentication service using the dependencyscope (this is in request scope)
            _authenticationService = request.GetDependencyScope().GetService(typeof(IAuthenticationService)) as IAuthenticationService;
            
            if (_authenticationService == null)
            {
                throw new ApplicationException("Authentication service cannot be resolved");
            }

            // Try get authentication header
            var authorisationHeader = request.Headers.Authorization;

            // If authentication header is available and is set to basic authentication
            if (authorisationHeader != null && Scheme.Equals(authorisationHeader.Scheme))
            {
                string username;
                string password;
                
                try
                {
                    username = authorisationHeader.Username();
                    password = authorisationHeader.Password();
                }
                catch
                {
                    // If authorization header cannot be properly read, give back sensible error
                    return request.CreateResponse(HttpStatusCode.BadRequest, "Authorization header is not properly formatted.");
                }

                // Authenticate request
                var authenticateResponse = _authenticationService.IsAuthenticated(username, password);

                // Set user if authentication is successfull
                if (authenticateResponse)
                {
                    var claimsIdentity = new ClaimsIdentity(AuthenticationTypes.Password);
                    claimsIdentity.AddClaim(new Claim(ClaimTypes.Name, username));

                    // Do not use thread.principal or httpcontext to set user. This will create dependency on system.web, which stands in the way of self hosting
                    // http://brockallen.com/2013/10/27/host-authentication-and-web-api-with-owin-and-active-vs-passive-authentication-middleware/
                    request.GetRequestContext().Principal = new ClaimsPrincipal(claimsIdentity);
                }
            }

            // Process request
            var response = await base.SendAsync(request, cancellationToken);

            // Add header to indicate basic authentication is needed when request is unauthorized
            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                response.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue(Scheme));
                response.Content = new JsonContent(new { Error = "Unauthorized" });
            }

            return response;
        }
开发者ID:robinvanderknaap,项目名称:PizzaApi,代码行数:57,代码来源:AuthenticationHandler.cs


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