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


C# IUserService.Update方法代码示例

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


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

示例1: AuthorizeCore

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            bool disableAuthentication = false;
            GenericIdentity identity;
            GenericPrincipal principal;
            string[] roles;

            #if DEBUG
            disableAuthentication = true;
            #endif

            if (disableAuthentication)
            {
                // Create a fake user and use this in debugging to disable authentication.
                identity = new GenericIdentity("alongoria");
                roles = new string[] { "admin" };
                principal = new GenericPrincipal(identity, roles);
                httpContext.User = principal;
            }
            else
            {
                HttpCookie authCookie = httpContext.Request.Cookies[FormsAuthentication.FormsCookieName];

                if (authCookie != null)
                {
                    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                    string username = authTicket.Name;

                    // According to many articles and Ninject docs like:
                    // https://github.com/ninject/Ninject.Web.Mvc/wiki/Filters-and-Scoped,
                    // https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=98,
                    // https://stackoverflow.com/questions/29915192/unity-property-injection-on-authorizeattribute
                    // MVC caches filters, which means dependency injection with .InRequestScope() does not work.
                    // If we try to inject the _authorizationFilter with Ninject, there is a runtime error saying the DbContext has been disposed
                    // on the second and all subsequent requests (works fine on the first request).
                    using (_userService = new UserService())
                    {
                        User user = _userService.GetByUsername(username);

                        if (user == null)
                        {
                            // The user has a cookie, so they are in the Active Directory.
                            // But they aren't in our local database (new employee, probably), so add them.

                            User newUser = new User()
                            {
                                Username = username,
                                IsActive = true,
                                IsAdmin = false
                            };

                            HttpCookie initialsCookie = httpContext.Request.Cookies[Configuration.GetAppSetting("UserInitialsCookieName")];

                            if (initialsCookie != null && !string.IsNullOrWhiteSpace(initialsCookie.Value))
                            {
                                newUser.Initials = initialsCookie.Value;
                            }
                            else
                            {
                                if (username.Length > 1)
                                    newUser.Initials = username.Substring(0, 2);
                                else
                                    newUser.Initials = "xx";
                            }

                            _userService.Insert(newUser);
                            user = _userService.GetByUsername(username);

                            using (var log = new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger())
                            {
                                log.Information("A new user was added to the application: {0}", username);
                            }
                        } // End inserting new User and pulling it from the db.
                        else
                        {
                            // If there's a cookie with initials, check its value to make sure our db value isn't out of date.

                            HttpCookie initialsCookie = httpContext.Request.Cookies[Configuration.GetAppSetting("UserInitialsCookieName")];
                            if (initialsCookie != null && !string.IsNullOrWhiteSpace(initialsCookie.Value))
                            {
                                if (!initialsCookie.Value.Equals(user.Initials, System.StringComparison.CurrentCultureIgnoreCase))
                                {
                                    user.Initials = initialsCookie.Value;
                                    _userService.Update(user);
                                }
                            }
                        }

                        if (!user.IsActive)
                        {
                            using (var log = new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger())
                            {
                                log.Warning("A user whose account was disabled attempted to log on to the application: {0}", username);
                            }

                            throw new HttpException(500, string.Format("The {0} account has been disabled.", authTicket.Name));
                        }

                        if (user.IsAdmin)
                            roles = new string[] { "admin" };
//.........这里部分代码省略.........
开发者ID:txsll,项目名称:SLLInvoices,代码行数:101,代码来源:CustomAuthorize.cs


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