Authenticating ASP.NET MVC user from a WPF application

后端 未结 2 1670
孤城傲影
孤城傲影 2021-02-06 19:14

How can I authenticate a user (with username and password) of an ASP.NET MVC application? I\'m trying to do this using WebClient, passing NetworkCredentials

2条回答
  •  清歌不尽
    2021-02-06 19:52

    Forms authentication works in two steps:

    1. The user goes to the LogIn page and enters his username and password and sends them to the server
    2. The server verifies them and if they are correct it emits an authentication cookie which is sent to the client. The client stores this cookie and sends it along each subsequent requests to the server.

    So to achieve this in a WPF application you will need to first obtain an authentication cookie. So first send a POST request to the LogIn page along with the username and password and read the returned cookie (For this you need to set the CookieContainer property on the HttpWebRequest for it to be able to catch the cookie). Once you have the cookie you reuse the cookie container in subsequent calls to authenticated pages.

    You may checkout this sample code to assist you (just replace the addresses and parameter names).

提交回复
热议问题