11

我想要 ASP.NET 中表单身份验证的好处。我希望它为我等保持授权,但我的情况有一点不同;我想针对一个简单的 Web 服务(具体由客户端提供)进行身份验证。

我有我的代码来查看网站,看看他们是否应该被授权,但是我如何在他们知道当前用户被授权的 ASP.NET 中设置 cookie[?] 或授权标志。

基本上...

if (HttpContext.Current.User.Identity.IsAuthenticated)
// we're all good

//Other wise...
bool success = CheckClientsWebService(string username, string password);

if (success)
// Somehow tell .NET that they're authorized

*注意:这是一个相当简单的服务,不涉及组或角色。只需检查用户是否可以查看该站点。

4

3 回答 3

10

在表单身份验证中不能证明您在表单身份验证 cookie 中的身份。?考虑到这一点,您不能在自定义登录表单中创建票证而不必创建自定义提供程序吗?我肯定认为你可以。进行快速测试并创建表单身份验证票证,并查看开箱即用的成员资格提供商是否认为用户已通过身份验证。

我很好奇 - 所以这里有一些代码..

模型

public class SignInViewModel
{
    public string Username { get; set; }
    public string Password { get; set; }
}

控制器

public class SignInController : Controller
{

    public ActionResult Index()
    {
        var model = new SignInViewModel {};
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(SignInViewModel model)
    {
        if (model.Username == "Fred" && model.Password == "Mertz")
        {
            FormsAuthentication.SetAuthCookie(model.Username, false);
            return RedirectToAction("Secure");
        }
        return View(model);
    }

    [Authorize]
    public ActionResult Secure(SignInViewModel model)
    {
        return View();
    }

    [Authorize]
    public ActionResult Logout(SignInViewModel model)
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Index");
    }

索引.cshtml

@using (Html.BeginForm()) {
    <fieldset>
        <legend>SignInViewModel</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Username)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Username)
            @Html.ValidationMessageFor(model => model.Username)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>

        <p>
            <input type="submit" value="Login" />
        </p>
    </fieldset>
}

安全.cshtml

<h2>Secure</h2>
@Html.ActionLink("Logout", "Logout")
于 2012-08-31T00:42:24.277 回答
7

我可能过度简化了这一点,但我阅读的方式如下:

  1. 如果用户未通过身份验证,则您有一个收集用户名/密码的表单
  2. 该表单的结果将传递给 Web 服务以进行授权
  3. 如果该授权成功,您需要一种方法让 Web 应用程序知道他们已经登录。
  4. 如果他们经过身份验证,请做一些事情

如果以上是正确的,您不需要会员提供商。[Authorize] 属性只是查看表单身份验证 cookie 以查看它是否已设置并且在 cookie 的当前生命周期内有效。此身份验证 cookie 存储用户的用户名和 cookie 的到期时间(以及其他内容,但在这里并不重要)。

鉴于此,您只需要设置 web.config 配置元素并具有设置身份验证 cookie 的方法。

网络配置

<system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>
</system.web>

登录 URL GET 操作

public ActionResult Logon(){
   //if the user is logged in, send the to the home page
   if(httpContext.User.Identity.IsAuthenticated_{
        Return RedirectToAction("Index", "Home");
   }
   Return this.View(new LoginViewModel());
}

登录 URL POST 操作

[HttpPost]
public ActionResult Logon(LoginViewModel model){
   //Check for model errors
   if(!ModelState.IsValid()){
       Return this.View(model);
   }

   //Validate against web service - return error if false
   if(!CheckClientsWebService(model.UserName, model.Password)){
       ModelState.AddModelError("","The username or password is invalid");
       Return this.View(model);
   } 

   //Manually set the authentication cookie
   FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);  
   //Send them on to the home page, they now have a authorization cookie
   Return RedirectToAction("Index", "Home");
}

调用该.SetAuthCookie()函数后,用户现在将拥有一个身份验证票,并且HttpContext.User.Identity.IsAuthenticated只要 cookie 未过期,调用 to 就会为真,并且您可以从中获取用户名HttpContext.User.Identity.Name

于 2012-08-31T04:39:35.857 回答
0

正如 Wiktor 评论的那样,实现您自己的MembershipProvider。只需实现您需要的方法,其余的扔一个NotImplementedException.

在您的情况下,您似乎只需要实现public bool ValidateUser(string username, string password)- 其实现只需要调用您的 Web 服务。

然后,您可以使用所有标准的内置身份验证和授权内容。

于 2012-08-30T21:16:05.900 回答