簡體   English   中英

來自具有身份標識2.0的桌面客戶端的SignalR身份驗證

[英]SignalR authentication from desktop client with identity 2.0

今年秋天,我想學習和編寫客戶端服務器。 秋天結束了,應用程序不存在,所以我要求一些幫助。 我仍然不明白如何從Signalr中心使用Identity 2.0登錄。

如果要連接到集線器,請像

hub.invoke("UsualLogin",login,password) //服務器檢查數據庫並發送回用於登錄狀態的代碼並進行身份驗證。

我也想做hub.invoke("GoogleLogin") //服務器要求google進行身份驗證,如果需要將smth發送回打開的瀏覽器,但是我不希望打開瀏覽器進行常規登錄。 我什至不希望網頁存在。

我閱讀了大量信息,觀看了數十個示例,但不知道如何執行此操作。 您能否舉一個僅連接到集線器,進行身份驗證並與自己進行[授權]聊天的示例? 這樣我就可以看到它是如何完成的以及在哪里完成的。

附:我不是一個懶惰的學生,這只是為了我的個人項目。

谷歌搜索signalr authentication的第一個結果是:

SignalR集線器的身份驗證和授權-ASP.Net http://www.asp.net/signalr/overview/security/hub-authorization

其中恰好包含您要的示例...

class Program
{
    static void Main(string[] args)
    {
        var connection = new HubConnection("http://www.contoso.com/");
        Cookie returnedCookie;

        Console.Write("Enter user name: ");
        string username = Console.ReadLine();

        Console.Write("Enter password: ");
        string password = Console.ReadLine();

        var authResult = AuthenticateUser(username, password, out returnedCookie);

        if (authResult)
        {
            connection.CookieContainer = new CookieContainer();
            connection.CookieContainer.Add(returnedCookie);
            Console.WriteLine("Welcome " + username);
        }
        else
        {
            Console.WriteLine("Login failed");
        }    
    }

    private static bool AuthenticateUser(string user, string password, out Cookie authCookie)
    {
        var request = WebRequest.Create("https://www.contoso.com/RemoteLogin") as HttpWebRequest;
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.CookieContainer = new CookieContainer();

        var authCredentials = "UserName=" + user + "&Password=" + password;
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(authCredentials);
        request.ContentLength = bytes.Length;
        using (var requestStream = request.GetRequestStream())
        {
            requestStream.Write(bytes, 0, bytes.Length);
        }

        using (var response = request.GetResponse() as HttpWebResponse)
        {
            authCookie = response.Cookies[FormsAuthentication.FormsCookieName];
        }

        if (authCookie != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM