MVC ASP.NET – 手动授权某人并通过表单身份验证保留授权

我想要ASP.NET中的表单身份验证的好处.我希望它能继续授权我这样,但是我的情况有一点不同;我想对一个简单的Web服务(特别是由客户端提供)进行身份验证.

我有我的代码来查看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

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

解决方法

在表单中,身份验证不能证明您是谁在表单身份验证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");
    }

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>
}

Secure.cshtml

<h2>Secure</h2>
@Html.ActionLink("logout","logout")

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...