MVC ASP.NET-手动授权某人并通过表单身份验证持久化授权
我想要ASP.NET中表单身份验证的好处。我希望它能坚持对我的授权,但我的情况有一点不同;我想针对一个简单的web服务(具体由客户机提供)进行身份验证 我有我的代码来查看web站点,看看他们是否应该被授权,但是如何在ASP.NET中设置cookie[?]或授权标志,让他们知道当前用户已被授权 基本上MVC ASP.NET-手动授权某人并通过表单身份验证持久化授权,asp.net,asp.net-mvc-3,forms-authentication,Asp.net,Asp.net Mvc 3,Forms Authentication,我想要ASP.NET中表单身份验证的好处。我希望它能坚持对我的授权,但我的情况有一点不同;我想针对一个简单的web服务(具体由客户机提供)进行身份验证 我有我的代码来查看web站点,看看他们是否应该被授权,但是如何在ASP.NET中设置cookie[?]或授权标志,让他们知道当前用户已被授权 基本上 if (HttpContext.Current.User.Identity.IsAuthenticated) // we're all good //Other wise... bool succ
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
*注意:这是一个相当简单的服务,不处理组或角色。只需检查用户是否可以查看该站点。正如Wiktor评论的那样,实现您自己的。只需实现所需的方法,剩下的部分抛出一个
NotImplementedException
在您的情况下,您似乎只需要实现public bool ValidateUser(字符串用户名、字符串密码)
——它的实现只需要调用您的Web服务即可
然后您可以使用所有标准的内置身份验证和授权工具。表单中的身份验证并不能证明您在表单身份验证cookie中是谁。?考虑到这一点,您不能在不创建自定义提供程序的情况下以自定义登录表单创建票据吗?我肯定认为你可以。进行快速测试并创建表单身份验证票证,查看开箱即用的成员资格提供程序是否认为用户已通过身份验证 我很好奇,这里有一些代码 型号
public class SignInViewModel
{
public string Username { get; set; }
public string Password { get; set; }
}
@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>
}
<h2>Secure</h2>
@Html.ActionLink("Logout", "Logout")
控制器
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
public class SignInViewModel
{
public string Username { get; set; }
public string Password { get; set; }
}
@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>
}
<h2>Secure</h2>
@Html.ActionLink("Logout", "Logout")
@使用(Html.BeginForm()){
符号视图模型
@LabelFor(model=>model.Username)
@EditorFor(model=>model.Username)
@Html.ValidationMessageFor(model=>model.Username)
@LabelFor(model=>model.Password)
@EditorFor(model=>model.Password)
@Html.ValidationMessageFor(model=>model.Password)
}
Secure.cshtml
public class SignInViewModel
{
public string Username { get; set; }
public string Password { get; set; }
}
@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>
}
<h2>Secure</h2>
@Html.ActionLink("Logout", "Logout")
安全
@ActionLink(“注销”、“注销”)
我可能过于简化了,但我的阅读方式如下:
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
</system.web>
登录URL发布操作
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());
}
[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()
函数后,用户现在将拥有身份验证票证,只要cookie尚未过期,并且您可以从HttpContext.user.Identity.IsAuthenticated
获取用户名,那么调用HttpContext.user.Identity.name
听起来您需要一个自定义成员资格提供程序来检查数据库中的身份验证在web服务中也是如此。然而,这是如此简单的解决方案,我认为我错过了一些重要的东西;我甚至不需要数据库检查,我只是查看一个web服务,看看用户名/密码是否正确。只需要知道如何保持用户的“身份验证”。您必须创建自己的成员资格提供商并发布表单身份验证cookie。这是一个标准的东西,因为你不需要处理组或角色,只需手动设置FuffSoNoTeTiTcCuffy就行了。有关详细信息,请参见.Hi,FormsAuthentication.SetAuthCookie(model.Username,false);他不适合我。我正在使用MVC5@RahulPatel您正在使用System.Web.Security命名空间吗?MVC5/ASP.NET 4.5+@RahulPatel应该支持它-MVC 5默认启用ASP.NET标识,这将禁用FormsAuthentication。