2013-04-03 47 views
2

所以我在做什麼看起來很簡單,但我不知道如何去做。如何將UserId添加到ASP.NET MVC中登錄用戶的發佈數據4

我已經註冊並使用帳戶登錄(我正在使用ASP.NET MVC 4中使用的默認成員系統),所以我想將我的UserId添加到我插入數據庫的一些數據中。

這是我在插入數據的模型:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace Reroute.Models 
{ 
    public class Request 
    { 
     public int  RequestId { get; set; } 
     // I want to add UserId based on my current session 
     public int  UserId { get; set; } 
     public string OrderNumber { get; set; } 
     public string TrackingNumber { get; set; } 
     public string CurrentAddress { get; set; } 
     public string NewAddress { get; set; } 
     public string Comment { get; set; } 
    } 
} 

而且ActionResult的(這裏的地方我認爲我必須做出改變):

[HttpPost] 
public ActionResult Create(Request collection) 
{ 
    try 
    { 
     _db.Requests.Add(collection); 
     _db.SaveChanges(); 
     //return RedirectToAction("Index"); 
     return Content("Done! Added to DB"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 

感謝

+0

你對登錄什麼機制?你在使用'FormsAuthentication'類嗎? –

+0

你面臨什麼問題/障礙? –

+0

@vonv。是的,FormsAuthentication – Danny

回答

0

使用這個它得到U中的用戶ID ...

Membership.GetUser().ProviderUserKey 
+0

更好,ty :) – Danny

0

登錄後,您可以將認證用戶的UserId保存在Session中:

Session["UserId"] = userId; 

或因爲您使用FormsAuthentication您可以使用的UserData屬性作爲shown here或做一個漂亮的,即,將-DO-招:

public SignInUser(string name, string id) { 
    // store the userid 
    FormsAuthentication.SetAuthCookie(name + '|' + id, false); 
} 

然後檢索名稱和用戶ID是這樣的:

public int CurrentUserId 
{ 
    get 
    { 
     var context = HttpContext.Current; 
     if (context == null) return 0; 

     return context.Request.IsAuthenticated 
        ? Convert.ToInt32(context.User.Identity.Name.Split('|')[1]) 
        : 0; 
    } 
} 

public string CurrentUserName 
{ 
    get 
    { 
     var context = HttpContext.Current; 
     if (context == null) return string.Empty; 

     return context.Request.IsAuthenticated 
      ? context.User.Identity.Name.Split('|')[0] 
      : string.Empty; 
    } 
} 

你可以在一個類中擁有這些方法和屬性,這樣你就可以將它們放在一個地方,實際上我是這麼做的。現在,你可以把它在你的控制器像這樣:

[HttpPost] 
public ActionResult Create(Request collection) 
{ 
    try 
    { 
     collection.UserId = _authProvider.CurrentUserId; 
     // if you want to use session, I prefer the FormsAuthentication approach 
     // you need to do additional check that the Session has not expired (not null) 
     collection.UserId = Session["UserId"]; 

     _db.Requests.Add(collection); 
     _db.SaveChanges(); 
     //return RedirectToAction("Index"); 
     return Content("Done! Added to DB"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 

_authProvider是有我上面給的代碼的類的實例。

0

這應該工作。

var loggedInUserName=Thread.CurrentPrincipal.Identity.Name; 
var user=Membership.GetUser(loggedInUserName); 
var key = user.ProviderUserKey; 

牛逼

+0

只用了3行! :) – Danny

0

假設你Create也有它裝起來,並作爲模型Create.cshtml一個GET,你只需要在ActionResult

public ActionResult Create() 
{ 
    Result model = new Result(); 
    model.UserId = myUserId; 
} 

明確設置它,然後在你的Create.cshtml你可以有一個隱藏的領域:

@Html.HiddenFor(m => m.UserId) 

我仍然會檢查POST以確保允許用戶進行保存並且不會將您的隱藏字段值僞造爲完全不同的人。

相關問題