2016-06-26 37 views
0

大廈聊天應用程序,我是新與SignalR,開始與Tutorial: Getting Started with SignalR 2 and MVC 5SignalR - 無法使用SignalR得到的用戶名

一切都過去了很好,現在我想impliment應用程序的用戶名,發件人,當我使用的方法是這樣的:

$('#displayname').val('@Context.User.Identity.Name'); 

中心獲取當前loged中的用戶名,並顯示頁面上,這是我的基地代碼:

[HubName("chatHub")] 
public class ChatHub : Hub 
{ 
    public void Send(string name, string message) 
    { 
     Clients.All.addNewMessageToPage(name, message); 
    } 

} 

現在我想impliment用戶名用另一種方法描述這種方法Here

下面的代碼無法正常工作,任何人都可以解釋爲什麼,我想知道如何使用SingalR的方法。 這裏是代碼不工作:感謝

[HubName("chatHub")] 
public class ChatHub : Hub 
{ 
    public void Send(string name, string message) 
    { 
     name = Context.User.Identity.Name; 
     Clients.All.addNewMessageToPage(name, message); 
    } 

} 

地塊和對不起我的英語。

回答

1

在你的啓動類中你配置了auth嗎?

像這樣的東西:

public void Configuration(IAppBuilder app) 
    { 
     var hubConfiguration = new HubConfiguration { EnableDetailedErrors = true }; 
     ConfigureAuth(app); 
     app.MapSignalR(hubConfiguration); 
    } 

當您創建與驗證的新MCV項目下面的代碼自動生成。

public void ConfigureAuth(IAppBuilder app) 
    { 

     // Configure the db context, user manager and signin manager to use a single instance per request 
     app.CreatePerOwinContext(ApplicationDbContext.Create); 
     app.CreatePerOwinContext<AppUserManager>(AppUserManager.Create); 
     app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create); 
     app.CreatePerOwinContext<AppRoleManager>(AppRoleManager.Create); 

     // Enable the application to use a cookie to store information for the signed in user 
     // and to use a cookie to temporarily store information about a user logging in with a third party login provider 
     // Configure the sign in cookie 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 

      ... 
     } 
}