コード例 #1
0
        public GenericResult <string> Login(UserModel userModel, HttpResponseMessage responce, HttpRequestMessage request)
        {
            var result = new GenericResult <string>();

            try
            {
                using (var db = new RssAggregatorModelContainer())
                {
                    var user = db.GetEntity <User>(el => el.IsActive && el.Name.ToLower() == userModel.Name.ToLower() && el.Password.ToLower() == userModel.Password.ToLower());
                    if (user != null)
                    {
                        result.SetDataResult(user.Name);

                        var cookie = new CookieHeaderValue(DEFAULT_COOKIE_NAME, user.UserKey)
                        {
                            Domain = request.RequestUri.Host,
                            Path   = "/"
                        };

                        if (userModel.CreateCookie.HasValue && userModel.CreateCookie.Value)
                        {
                            cookie.Expires = DateTimeOffset.UtcNow.AddDays(COOKIE_EXPIRE_DAYS);
                        }

                        responce.Headers.AddCookies(new[] { cookie });
                    }
                    else
                    {
                        result.SetWarningResultCode(SettingService.GetSetting("LOGIN_IncorrectUserNameOrPassword"));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }