/// <summary> /// Connects to the dotNetify hub server. /// </summary> /// <param name="vmId">Identifies the view model to request.</param> /// <param name="viewState">View state manager.</param> /// <param name="options">View model initialization options.</param> public async Task ConnectAsync(string vmId, IViewState viewState, VMConnectOptions options = null) { if (!string.IsNullOrEmpty(_vmId)) { throw new Exception($"The instance was connected to '{_vmId}'. Call Dispose to disconnect."); } _vmId = vmId; _viewState = viewState; await _hubProxy.StartAsync(); _hubProxy.Response_VM += OnResponseReceived; Dictionary <string, object> data = null; if (options?.VMArg != null) { data = new Dictionary <string, object>(); data.Add(TOKEN_VMARG, options.VMArg); if (options?.Headers != null) { data.Add(TOKEN_HEADERS, options.Headers); } } await _hubProxy.Request_VM(vmId, data); }
public void ExampleSecurePage_Connect_ReplaceToken_ReturnsCorrectUser() { var expireSeconds = 5; var client = _hubEmulator.CreateClient(); var options = new VMConnectOptions(); options.Headers.Set("Authorization", "Bearer " + CreateBearerToken("john", "guest", expireSeconds)); client.Connect(nameof(SecurePageVM), options); client.Listen(1000); var state = client.GetState <ClientState>(); Assert.AreEqual("Authenticated user: \"john\"", state.SecureCaption); client.Dispatch(new Dictionary <string, object> { { "$headers", new { Authorization = "Bearer " + CreateBearerToken("bob", "guest", expireSeconds) } }, { "Refresh", "true" } }); client.Listen(1000); state = client.GetState <ClientState>(); Assert.AreEqual("Authenticated user: \"bob\"", state.SecureCaption); }
public void ExampleSecurePage_Connect_Multiple_ReturnsCorrectUser() { var expireSeconds = 5; var client1 = _hubEmulator.CreateClient(); var client2 = _hubEmulator.CreateClient(); var options1 = new VMConnectOptions(); options1.Headers.Set("Authorization", "Bearer " + CreateBearerToken("john", "guest", expireSeconds)); var options2 = new VMConnectOptions(); options2.Headers.Set("Authorization", "Bearer " + CreateBearerToken("bob", "admin", expireSeconds)); client1.Connect(nameof(SecurePageVM), options1); client2.Connect(nameof(SecurePageVM), options2); client1.Listen(1000); client2.Listen(1000); var state1 = client1.GetState <ClientState>(); var state2 = client2.GetState <ClientState>(); Assert.AreEqual("Authenticated user: \"john\"", state1.SecureCaption); Assert.AreEqual("Authenticated user: \"bob\"", state2.SecureCaption); }
public void RuntimeVM_RequestWithNamespace() { var options = new VMConnectOptions(); options.VMArg.Set("namespace", "MyNamespace"); var client = _hubEmulator.CreateClient(); var response = client.Connect("HelloWorldRuntimeVM", options).As <dynamic>(); Assert.AreEqual("John", (string)response.FirstName); Assert.AreEqual("Hancock", (string)response.LastName); Assert.AreEqual("John Hancock", (string)response.FullName); }
public void ExampleSecurePage_Connect_GuestAuthToken_ReturnsSecureData() { var expireSeconds = 3; var client = _hubEmulator.CreateClient(); var options = new VMConnectOptions(); options.Headers.Set("Authorization", "Bearer " + CreateBearerToken("john", "guest", expireSeconds)); client.Connect(nameof(SecurePageVM), options); var responses = client.Listen(expireSeconds * 1000); var state = client.GetState <ClientState>(); Assert.AreEqual("Authenticated user: \"john\"", state.SecureCaption); Assert.IsTrue(state.SecureData.StartsWith("Access token will expire in")); }
public void Initialize() { string secretKey = "dotnetifydemo_secretkey_123!"; _tokenValidationParameters = new TokenValidationParameters { IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey)), ValidAudience = "DotNetifyDemoApp", ValidIssuer = "DotNetifyDemoServer", ValidateIssuerSigningKey = true, ValidateAudience = true, ValidateIssuer = true, ValidateLifetime = true, ClockSkew = TimeSpan.FromSeconds(0) }; _vmConnectOptions = new VMConnectOptions(); _vmConnectOptions.VMArg.Set("Property", "World"); _vmConnectOptions.Headers.Set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZG1pbiIsImp0aSI6IjI0YTUwOGJlLWJlMTktNDFhZS1iZmI1LTc5OGU4YmNjNDI3ZCIsImlhdCI6MTUxNDUyODgxNiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6WyJhZG1pbiIsImFkbWluIl0sImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6ImFkbWluIiwibmJmIjoxNTE0NTI4ODE2LCJleHAiOjE4Mjk4ODg4MTYsImlzcyI6IkRvdE5ldGlmeURlbW9TZXJ2ZXIiLCJhdWQiOiJEb3ROZXRpZnlEZW1vQXBwIn0.q2wZyS13FskQ094O9xbz4FLlRPPHf1GfKOUOTHJyLbk"); }
public void ExampleSecurePage_Connect_SystemTextJsonProtocol_ReturnsSecureData() { var expireSeconds = 3; var client = _hubEmulator.CreateClient(); var vmConnectOptions = new VMConnectOptions(); vmConnectOptions.Headers.Set("Authorization", "Bearer " + CreateBearerToken("john", "guest", expireSeconds)); var serializedString = ((JObject)vmConnectOptions).ToString(); object options = JsonSerializer.Deserialize <object>(serializedString); client.Connect(nameof(SecurePageVM), options); var responses = client.Listen(expireSeconds * 1000); var state = client.GetState <ClientState>(); Assert.AreEqual("Authenticated user: \"john\"", state.SecureCaption); Assert.IsTrue(state.SecureData.StartsWith("Access token will expire in")); }
public async Task DotNetifyClient_ConnectAsyncWithOptions() { var mockHubProxy = Substitute.For <IDotNetifyHubProxy>(); var sut = new DotNetifyClient(mockHubProxy, new DefaultUIThreadDispatcher()); var options = new VMConnectOptions { VMArg = new Dictionary <string, object> { { "FirstName", "Hello" } }, Headers = new { OrganizationId = "MyOrgId" } }; var helloWorldVM = new HelloWorldVM(); await sut.ConnectAsync(nameof(HelloWorldVM), helloWorldVM, options); await mockHubProxy.Received().StartAsync(); await mockHubProxy.Received().Request_VM(nameof(HelloWorldVM), Arg.Is <Dictionary <string, object> >(x => x.ContainsKey("$vmArg") && x["$vmArg"] == options.VMArg && x.ContainsKey("$headers") && x["$headers"] == options.Headers )); }
/// <summary> /// Connects to the dotNetify hub server. /// </summary> /// <param name="vmId">Identifies the view model to request.</param> /// <param name="view">The connecting view; must implement changed notification.</param> /// <param name="options">View model initialization options.</param> public async Task ConnectAsync(string vmId, object view, VMConnectOptions options = null) { await ConnectAsync(vmId, new ViewState(view, _dispatcher), options); }