0

クライアントが認証されている場合にのみサービスにアクセスできるようにしようとしていますが、認証属性を設定しましたが、認証されずにサービスにアクセスできるため、機能しませんでした。Authenticate 属性を Request DTO の前、サービスの上、Action の前に配置しました。保護したいサービスのコードを次に示します

[Authenticate]
public class HelloService : Service
{
    public const string HelloServiceCounterKey = "HelloServiceCounter";

    public object Any(HelloRequest request)
    {
            var userSession = SessionAs<AppHost.CustomUserSession>();
            Session.Set(HelloServiceCounterKey, Session.Get<int>(HelloServiceCounterKey) + 1);
            var roles = string.Join(", ", userSession.Roles.ToArray());
            return new HelloResponse { Result = "Hello, " + request.Name + ", your role(s): " + roles };

    }
}

AppHost Configure(Funq.Container container) にこれがあります

Plugins.Add(new AuthFeature(
           () => new CustomUserSession(),
           new[] { new CustomCredentialsAuthProvider() }
       ));

public class CustomUserSession : AuthUserSession
    {
        public string CompanyName { get; set; }
    }

    public class CustomCredentialsAuthProvider : CredentialsAuthProvider
    {
        public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
        {
            if (!Membership.ValidateUser(userName, password)) return false;

            var session = (CustomUserSession)authService.GetSession(false);
            session.CompanyName = "Company from DB";
            session.UserAuthId = userName;
            session.IsAuthenticated = true;

            // add roles 
            session.Roles = new List<string>();
            if (session.UserAuthId == "admin") session.Roles.Add(RoleNames.Admin);
            session.Roles.Add("User");

            return true;
        }
    }

の回線でサービスにアクセスすると

var roles = string.Join(", ", userSession.Roles.ToArray());

認証されていないため、明らかに NULL を返します。

この場合、認証属性は何をすべきですか?

4

1 に答える 1

1

次のように、アプリのホスト構成で認証プロバイダーを構成する必要があります。

public override void Configure(Container container)
{ 
    Plugins.Add(new AuthFeature(() => new AuthUserSession(), new IAuthProvider[]
                {
                 your providers here...
                 }));

}

編集:

CustomUserSession が IAuthSession から継承すると仮定すると、変更できます

 var session = (CustomUserSession)authService.GetSession(false);

 var session = authService.GetSession<CustomUserSession>();

私が見る限り、認証後にセッションを保存していません

このようなことを試してください:

public override object Authenticate(IServiceBase authService, IAuthSession session, ServiceStack.ServiceInterface.Auth.Auth request)
        {

            string userName = request.UserName;
            string password = request.Password;

            if (!LoginMatchesSession(session, userName))
            {
                authService.RemoveSession();
                session = authService.GetSession();
            }

            if (TryAuthenticate(authService, userName, password))
            {
                authService.SaveSession(session, SessionExpiry);
                if (session.UserAuthName == null)
                    session.UserAuthName = userName;
                OnAuthenticated(authService, session, null, null);

                return new AuthResponse
                {
                    UserName = userName,
                    SessionId = session.Id,
                    ReferrerUrl = RedirectUrl
                };
            }

            throw new HttpError(HttpStatusCode.BadRequest, "400", "wrong credentials");

        }


  public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
        {
            var session = authService.GetSession<CustomUserSession>();
           if (!Membership.ValidateUser(userName, password)) return false;


                session.IsAuthenticated = true;
                session.Id = authService.GetSessionId();
                return true;


            }

編集: すべてのセッションがキャッシュで管理されるため、キャッシュクライアントを構成するために必要な他の欠落部分があります

そのようなことを試してください:

container.Register<ICacheClient>(new MemoryCacheClient(){FlushOnDispose = false});

ホスト構成でコードを更新できますか?

于 2013-11-14T19:47:52.197 に答える