从WPF应用程序认证ASP.NET MVC用户 [英] Authenticating ASP.NET MVC user from a WPF application

查看:303
本文介绍了从WPF应用程序认证ASP.NET MVC用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何验证用户(用户名和密码)ASP.NET MVC应用程序吗?我试图做到这一点使用 Web客户端,通过 NetworkCredentials ,发帖请求从ASP.NET MVC应用程序我的WPF客户端。如何处理在服务器上的这一要求?我如何获得通过用户名和密码?

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, posting the request to the ASP.NET MVC application from my WPF client. How do I handle this request on the server? How do I get the passed username and password?

我使用的ASP.NET MVC应用程序窗体身份验证(即一个新项目创建的默认)。

I'm using forms authentication in the ASP.NET MVC app (the default that is created with a new project).

推荐答案

Forms身份验证两个步骤如下:

Forms authentication works in two steps:


  1. 的用户进入登录页面,输入自己的用户名和密码,并将它们发送到服务器

  2. 服务器验证他们,如果它们是正确其发射被发送到客户端的认证的cookie。在客户端存储此cookie并将其沿每个后续请求到服务器。

所以要在WPF应用程序,你需要先获得验证cookie实现这一目标。所以先用用户名和密码一起发送POST请求到登录页面,并读取返回的cookie(为此,你需要设置在HttpWebRequest的的的CookieContainer属性它能够赶上的cookie)。一旦你的饼干,你重复使用的cookie容器后续调用验证页面。

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.

您可以签<一个href=\"http://stackoverflow.com/questions/2798610/login-to-website-and-use-cookie-to-get-source-for-another-page/2798796#2798796\">this样品code 来帮助你(只需更换地址和参数名称)。

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

这篇关于从WPF应用程序认证ASP.NET MVC用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆