C# MVC4应用程序和WCF:如何创建自定义身份验证?

C# MVC4应用程序和WCF:如何创建自定义身份验证?,c#,sql-server,wcf,asp.net-mvc-4,razor,C#,Sql Server,Wcf,Asp.net Mvc 4,Razor,我正在使用VisualStudio,我创建了一个MVC4应用程序项目,其中包含一个身份验证部分(默认部分)。 另一方面,我有一个web服务(WCF),它喜欢由SQL server管理的数据库,其中包含用户(登录名、密码)表。 我想将“默认”身份验证更改为链接到web服务提供的数据的身份验证。 因此,需要明确的是:如何查询远程数据库以检查登录/pwd的正确性 只是想让你了解已经做了什么: 会计控制员: [HttpPost] [AllowAnonymous] [Va

我正在使用VisualStudio,我创建了一个MVC4应用程序项目,其中包含一个身份验证部分(默认部分)。 另一方面,我有一个web服务(WCF),它喜欢由SQL server管理的数据库,其中包含用户(登录名、密码)表。 我想将“默认”身份验证更改为链接到web服务提供的数据的身份验证。 因此,需要明确的是:如何查询远程数据库以检查登录/pwd的正确性

只是想让你了解已经做了什么: 会计控制员:

 [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }

            ModelState.AddModelError("", "Login or password not correct");
            return View(model);
        }
登录模型:

public class LoginModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }
    }
登入表格:

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <ol>
        <li>
            @Html.LabelFor(m => m.UserName)
            @Html.TextBoxFor(m => m.UserName)
            @Html.ValidationMessageFor(m => m.UserName)
        </li>
        <li>
            @Html.LabelFor(m => m.Password)
            @Html.PasswordFor(m => m.Password)
            @Html.ValidationMessageFor(m => m.Password)
        </li>
    </ol>
    <input type="submit" value="Login" />
ligne 45在哪里

List<SteelcaseWebPortal.SteelcaseService.PERSON> lpers = usr.ToList();
List lpers=usr.ToList();

我的请求有没有做错什么?谢谢

更简单的方法是将下面的方法更改为对web服务进行调用的自定义方法

WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe)
返回布尔值的新值:

MyWebService.Login(model.UserName, model.Password)
如果您希望将身份验证持久化到cookie级别。当if通过时,添加以下内容:

FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
MyWebService.Login(model.UserName, model.Password)
FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);